コード例 #1
0
ファイル: views.py プロジェクト: huashanqitu/blog1
 def post(self, request):
     comment_form = CommentForm(request.POST)
     if comment_form.is_valid():
         comment_form.save()
         return HttpResponse('{"status": "success"}', content_type='application/json')
     else:
         return HttpResponse('{"status": "fail"}', content_type='application/json')
コード例 #2
0
def add_comment_to_post(request, pk):
    """
    To add a comment when a 'request' with primary key('pk') of post given,
    :param request:
    :param pk:
    :return: the html tag 'form' and the form is created based on logic
    """
    # Grab the object or return a 404(not found) error
    post = get_object_or_404(Post, pk=pk)
    if request.method == 'POST':
        # If the form filled and request submitted
        # get the content to form
        form = CommentForm(request.POST)
        if form.is_valid():
            # checking the form valid or not,
            # if valid save to DB
            comment = form.save(commit=False)
            # post in Comment model foreignkey relation, attach comment to
            # this post
            comment.post = post
            comment.save()
            # after saving redirect to current post_detail page
            return redirect('myblog:post_detail', pk=post.pk)
    else:
        # the form is not posted stay on comment form
        form = CommentForm()
    # render out the html based on 'form' template tagging in
    # 'comment_form.html'
    return render(request, 'myblog/comment_form.html', {'form': form})
コード例 #3
0
ファイル: views.py プロジェクト: ahzb0565/DjangoBlog
def post_detail(request, year, month, day, post):
    '''
    post detail view
    '''
    post = get_object_or_404(Post,
                            slug = post,
                            status = 'published',
                            publish__year = year,
                            publish__month = month,
                            publish__day = day
                            )
    #list of active comments for this post
    comments = post.comments.filter(active = True)

    if request.method == "POST":
        #A comments was posted
        comment_form = CommentForm(request.POST)
        if comment_form.is_valid():
            # Create Comment object but don't save to database yet
            new_comment = comment_form.save(commit = False)
            # Assign the current post to the comment
            new_comment.post = post
            # Save the comment to the database
            new_comment.save()
    else:
        #get empty form
        comment_form = CommentForm()

    # List of similar posts
    post_tags_ids = post.tags.values_list('id', flat=True)
    similar_posts = Post.published.filter(tags__in=post_tags_ids).exclude(id=post.id)
    similar_posts = similar_posts.annotate(same_tags=Count('tags')).order_by('-same_tags','-publish')[:4]

    return render(request, "myblog/post/detail.html",
        {'post':post, 'comments': comments, 'comment_form':comment_form, 'similar_posts':similar_posts})
コード例 #4
0
ファイル: views.py プロジェクト: bglogowski/PyWeb-09
def view_post(request, slug):
    post = get_object_or_404(Post, slug=slug)
    form = CommentForm(request.POST or None)
    if form.is_valid():
        comment = form.save(commit=False)
        comment.post = post
        comment.save()
        return redirect(request.path)
    return render_to_response('blog_post.html', { 'post': post, 'form': form, }, context_instance=RequestContext(request))
コード例 #5
0
def add_comment_to_post(request, pk):
    post = get_object_or_404(Post, pk=pk)
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.post = post
            comment.save()
            return redirect('post_detail', pk=post.pk)
    else:
        form = CommentForm()
    return render(request, 'myblog/comment_form.html', {'form': form})
コード例 #6
0
def post_detail(request, year, month, day, post):
    '''
    post detail view
    '''
    post = get_object_or_404(Post,
                             slug=post,
                             status='published',
                             publish__year=year,
                             publish__month=month,
                             publish__day=day)
    #list of active comments for this post
    comments = post.comments.filter(active=True)

    if request.method == "POST":
        #A comments was posted
        comment_form = CommentForm(request.POST)
        if comment_form.is_valid():
            # Create Comment object but don't save to database yet
            new_comment = comment_form.save(commit=False)
            # Assign the current post to the comment
            new_comment.post = post
            # Save the comment to the database
            new_comment.save()
    else:
        #get empty form
        comment_form = CommentForm()

    # List of similar posts
    post_tags_ids = post.tags.values_list('id', flat=True)
    similar_posts = Post.published.filter(tags__in=post_tags_ids).exclude(
        id=post.id)
    similar_posts = similar_posts.annotate(same_tags=Count('tags')).order_by(
        '-same_tags', '-publish')[:4]

    return render(
        request, "myblog/post/detail.html", {
            'post': post,
            'comments': comments,
            'comment_form': comment_form,
            'similar_posts': similar_posts
        })