簡體   English   中英

django為什么找不到我的“正確”命名模板文件夾?

[英]why isn't django finding my “properly” named templates folder?

本教程中 ,有一個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”,那該如何發表評論呢? 如果它已經存在,我不明白為什么要再次添加它。 謝謝

這行沒有從數據庫中獲取評論,而是創建了一個新的評論實例。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM