[英]why post blog content get by comment?
hello i have a problem that when i post a Blog,it is got by both blog and comment, here is the specific: 你好,我有一个问题,当我发布博客时,博客和评论都可以得到,具体如下:
when i post a blog, the content goes to Model.Blog.content, but also goes to Model.Comments.content, and save in Mysql database, table Blog and Comments, show in front end either. 当我发布博客时,内容转到Model.Blog.content,但也转到Model.Comments.content,并保存在Mysql数据库,表Blog和Comments中,或者显示在前端。 i think it is there is some problem with the name"content",they are same in Blog and comment model, but not sense about that.
我认为“内容”这个名称存在一些问题,它们在Blog和评论模型中是相同的,但对此没有任何意义。
so, pls help, with great appreciate!!! 因此,请提供帮助,万分感谢!!!
url.py 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 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 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 })
comments template 评论模板
{% for comment in comments %}
<div class="comment-field" style="padding-top:10px;">
{{ comment.name }} 说:{{ comment }}
</div>
{% endfor %}
blog_post template 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>
In Post_blog
you have return get_details(request,blog_id=post.id)
. 在
Post_blog
您将return get_details(request,blog_id=post.id)
。 This calls get_details with POST request and create new comment. 这将通过POST请求调用get_details并创建新注释。 You should change it to
return redirect('blog_get_detail')
but dont forget to import redirect
first: 您应该更改它以
return redirect('blog_get_detail')
但不要忘记首先导入redirect
:
from django.shortcuts import redirect
return redirect('blog_get_detail', post.id)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.