Example #1
0
def post_detail_view(request, pk):

    posts = BlogPost.objects.all()
    all_tags = Tag.objects.all()

    post = BlogPost.objects.filter(pk=pk).first()
    next_post = BlogPost.objects.filter(pk=pk + 1).first()
    previous_post = BlogPost.objects.filter(pk=pk - 1).first()
    commentform = CommentForm()

    if request.method == 'POST':
        commentform = CommentForm(request.POST)
        if commentform.is_valid():
            pre_save_form = commentform.save(commit=False)
            pre_save_form.post = post
            pre_save_form.save()
            commentform = CommentForm()
            messages.success(request, 'your comment was saved successfully')

    context = {
        'post': post,
        'previous_post': previous_post,
        'next_post': next_post,
        'posts': posts,
        'all_tags': all_tags,
        'commentform': commentform
    }

    return render(request, 'blog/single-blog1.html', context)
Example #2
0
def view_blog(request, year, month, day, post):
    username = request.session.get('username')

    detail_post = get_object_or_404(Post,
                                    status='published',
                                    slug=post,
                                    publish__year=year,
                                    publish__month=month,
                                    publish__day=day)

    # '''For Comments section'''
    all_comments = detail_post.comments.all()
    csubmit = False
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            new_comment = form.save(commit=False)
            new_comment.post = detail_post
            new_comment.save()
            csubmit = True
    else:
        form = CommentForm()

    return render(
        request, 'blog/blog.html', {
            'post': detail_post,
            'form': form,
            'comments': all_comments,
            'csubmit': csubmit,
            'username': username
        })
Example #3
0
def post(request, pk):
    post = get_object_or_404(Post, pk=pk)
    form = CommentForm()
    if request.method == 'POST':
        form = CommentForm(request.POST, author=request.user, post=post)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(request.path)
    return render(request, "blog/post.html", {"post":post, "form":form})
Example #4
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, 'blog/comment_form.html', {'form': form})
Example #5
0
def add_comment_to_post(request, post_id):
    post = get_object_or_404(Post, id=post_id)
    author = request.user
    if request.method == "POST":
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.post = post
            comment.author = author
            comment.save()
            return redirect("/index/", post_id=post.id)
    else:
        form = CommentForm()
    return render(request, "add_comment_to_post.html", {'form' : form})
Example #6
0
def add_comment_to_post(request, post_id):
    post = get_object_or_404(Post, id=post_id)
    author = request.user
    if request.method == "POST":
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.post = post
            comment.author = author
            comment.save()
            return redirect("/index/", post_id=post.id)
    else:
        form = CommentForm()
    return render(request, "add_comment_to_post.html", {'form': form})
Example #7
0
def blog_detail(request, year, month, day, id):
    article = get_object_or_404(Article, pk=id)
    img_id = UserInfo.objects.get(id=1)
    str = img_id.img.url
    comments = Comment.objects.all()

    if request.method == 'GET':

        # 如果请求的方式是get  生产一个空的表单对象,通过上下文直接返回
        form = CommentForm()
        Context = {
            'detail_list': article,
            'img_index': str,
            'form': form,
            'comments': comments
        }
        return render(request, 'blog_datail.html', Context)
    elif request.method == 'POST':
        # 如果是post方式,那么先创建一个表单实例然后绑定数据到该表单,然后通过表单的is_valid()方法对form进行数据有效性性检验,
        form = CommentForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data
            Comment.objects.create(article=article,
                                   text=data['comments'],
                                   name=data['name'],
                                   email=data['email'])
            # 这里再次创建一个空的表单对象是因为,上一条的新增数据语句已经执行成功,再次生成空的对象是为了防止网页上新增成功之后
            # 数据依然残留在表单中的bug,(还有其他方式,这并不是一种最完美的解决方案)
            form = CommentForm()
            Context = {
                'detail_list': article,
                'img_index': str,
                'form': form,
                'comments': comments
            }
            messages.success(request, '留言成功!')
            return render(request, 'blog_datail.html', Context)
        else:
            # 如果数据的有效性建议并没有通过,同上一样,直接返回
            Context = {
                'detail_list': article,
                'img_index': str,
                'form': form,
                'comments': comments
            }
            messages.error(request, '留言失败')
            return render(request, 'blog_datail.html', Context)
Example #8
0
def get_details(request, blog_id):
    try:
        blog = Blog.objects.get(id=blog_id)
    except Blog.DoesNotExist:
        raise Http404
    if request.method == 'GET':
        form = CommentForm()
    else:
        form = CommentForm(request.POST)
        if form.is_valid():
            cleaned_data = form.cleaned_data
            cleaned_data['blog'] = blog
            Comment.objects.create(**cleaned_data)
    ctx = {
        'blog': blog,
        'comments': blog.comment_set.all().order_by('-created'),
        'form': form
    }
    return render(request, 'blog_details.html', ctx)
Example #9
0
def blog_detail(request, pk):
    post = Post.objects.get(pk=pk)

    form = CommentForm()
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = Comment(author=form.cleaned_data["author"],
                              body=form.cleaned_data["body"],
                              post=post)
            comment.save()

    comments = Comment.objects.filter(post=post)
    context = {
        "post": post,
        "comments": comments,
        "form": form,
    }
    return render(request, "blog_detail.html", context)
Example #10
0
def comment_get_detail(request, blog_id):
    try:
        blog = Blog.objects.get(id=blog_id)
    except:
        raise Http404
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            cleaned_data = form.cleaned_data  # 清洗数据,形成字典
            cleaned_data['blog'] = blog  # 加入blog信息
            Comment.objects.create(**cleaned_data)  # 加入Comment对象
            return HttpResponseRedirect('/detail/%s/' % blog_id)
    else:
        form = CommentForm()
    info_data = {
        'blog': blog,
        'comments':
        blog.comment_set.all().order_by('-pub'),  #comment_set是django中对外键的调用
        'form': form,
    }

    return render(request, 'comment_submit.html', info_data)