为什么发布博客内容得到评论?

时间:2017-02-05 09:00:03

标签: python django

你好,我有一个问题,当我发布博客时,它是由博客和评论得到的,这里是具体的:

问题

当我发布博客时,内容转到Model.Blog.content,但也转到Model.Comments.content,并保存在Mysql数据库中,表博客和评论,也显示在前端。我认为名称“内容”存在一些问题,它们在博客和评论模型中是相同的,但对此没有意义。

所以,请帮助,非常感谢!!!

url.py

urlpatterns = [
url(r'^admin/',admin.site.urls),
url(r'^blogs/$',get_blogs),
url(r'^detail/(\d+)/$',get_details,name='blog_get_detail'),
url(r'^$', Register, name='register'),
url(r'^register/$',Register,name='register'),
url(r'^blog_post/$',Post_blog,name='blog_post'),

models.py

class Blog(models.Model):
title = models.CharField(verbose_name='标题',max_length=150)
author = models.CharField(verbose_name='作者',max_length=16,blank=True, null=True,default='adma')
# abstract = models.TextField('blog_abstract',blank=True, null=True,max_length=150)
content = models.TextField(verbose_name='内容',max_length=5000)
created = models.DateTimeField(verbose_name='发布日期',default=datetime.datetime.now)
catagory = models.ForeignKey(Catagory,related_name="blog_catagory",verbose_name="分类")
tags = models.ManyToManyField(Tag,related_name="blog_tag",verbose_name="标签")

class Comment(models.Model):
blog = models.ForeignKey(Blog,verbose_name='文章')
name = models.CharField(verbose_name='评论人',max_length=16,blank=True, null=True)
# email = models.EmailField('email')
content = models.TextField(verbose_name='评论内容',max_length=240)
created = models.DateTimeField(verbose_name='评论时间',default=datetime.datetime.now)

view.py

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)

def Post_blog(request):
if request.method == "POST":
    bf = BlogForm(request.POST)

    if bf.is_valid():
        post = bf.save(commit=False)
        post.author = request.user
        post.save()
        return get_details(request,blog_id=post.id)
else:
    bf = BlogForm()
return render(request,'blog_post.html',{'bf':bf })

评论模板

    {% for comment in comments %}
            <div class="comment-field" style="padding-top:10px;">
                {{ comment.name }} 说:{{ comment }}
            </div>
        {% endfor %}

blog_post模板

  <form id="blog_form" enctype="multipart/form-data" action="" method="post">
            {% csrf_token %}
            <div>
                {{ bf.as_p}}
            </div>
            <input type="submit" value="保存并发布">
        </form>

1 个答案:

答案 0 :(得分:0)

Post_blog return get_details(request,blog_id=post.id)您有return redirect('blog_get_detail')。 这会使用POST请求调用get_details并创建新注释。 您应该将其更改为redirect,但不要忘记先导入from django.shortcuts import redirect return redirect('blog_get_detail', post.id)

gets