我是django的新手。我正在閱讀博客tutorial。從博客教程我無法理解以下部分。任何人都可以解釋我嗎?我將非常感激。感謝任何人都可以解釋我的django博客代碼(詳細內部)
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]))
有沒有你不明白或想了解更多的特定部分? – TomHarrigan
@TomHarrigan'如果p.has_key(「body」)和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) 。我無法理解這一點。 –