为什么 Django 找不到我的 "properly" 命名模板文件夹?
why isn't django finding my "properly" named templates folder?
在this tutorial中,有一个ModelForm
:
from django.forms import ModelForm
class CommentForm(ModelForm):
class Meta:
model = Comment
exclude = ["post"]
def add_comment(request, pk):
"""Add a new comment."""
p = request.POST
if p.has_key("body") and p["body"]:
author = "Anonymous"
if p["author"]:
author = p["author"]
comment = Comment(post=Post.objects.get(pk=pk))
cf = CommentForm(p, instance=comment)
cf.fields["author"].required = False
comment = cf.save(commit=False)
comment.author = author
comment.save()
return HttpResponseRedirect(reverse("dbe.blog.views.post", args=[pk]))
他们从哪里获得评论 comment = Comment(post=Post.objects.get(pk=pk))
?如果我们还没有制作或保存评论,怎么可能已经有评论了,而该功能的全部目的是"add_comment"?如果它已经存在,我不明白为什么我们要再次添加它。谢谢
这一行没有从数据库中获取评论,它是 creating 一个新的评论实例。
comment = Comment(post=Post.objects.get(pk=pk))
如果我们重写得更详细一些,可能会更容易理解:
post = Post.objects.get(pk=pk) # fetch the post based on the primary key
comment = Comment(post=post) # create a new comment (it is not saved at this point)
...
comment.save() # the comment is saved to the db
在this tutorial中,有一个ModelForm
:
from django.forms import ModelForm
class CommentForm(ModelForm):
class Meta:
model = Comment
exclude = ["post"]
def add_comment(request, pk):
"""Add a new comment."""
p = request.POST
if p.has_key("body") and p["body"]:
author = "Anonymous"
if p["author"]:
author = p["author"]
comment = Comment(post=Post.objects.get(pk=pk))
cf = CommentForm(p, instance=comment)
cf.fields["author"].required = False
comment = cf.save(commit=False)
comment.author = author
comment.save()
return HttpResponseRedirect(reverse("dbe.blog.views.post", args=[pk]))
他们从哪里获得评论 comment = Comment(post=Post.objects.get(pk=pk))
?如果我们还没有制作或保存评论,怎么可能已经有评论了,而该功能的全部目的是"add_comment"?如果它已经存在,我不明白为什么我们要再次添加它。谢谢
这一行没有从数据库中获取评论,它是 creating 一个新的评论实例。
comment = Comment(post=Post.objects.get(pk=pk))
如果我们重写得更详细一些,可能会更容易理解:
post = Post.objects.get(pk=pk) # fetch the post based on the primary key
comment = Comment(post=post) # create a new comment (it is not saved at this point)
...
comment.save() # the comment is saved to the db