2011-06-21 12 views
-1

私は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])) 
+2

あなたが理解していない、または詳細を知りたい特定の部分はありますか? – TomHarrigan

+0

@TomHarrigan 'p.has_key(" body ")とp [" body "]の場合: author ="匿名 " p [" author "]:author = p [" author "] コメント= コメント= cf.save(コミット= False) ''投稿者 ''が投稿されました。投稿者:投稿者:Post.objects.get(pk = pk)) cf = CommentForm(p、instance = comment) cf.fields ["author"]。 。私はこの点を理解できません。 –

答えて

3
class CommentForm(ModelForm): 
    class Meta: 
     model = Comment 
     exclude = ["post"] 

    def add_comment(request, pk): 
     """Add a new comment.""" 
     p = request.POST 

     # if POST has key "body" and p["body"] evalutes to True 
     if p.has_key("body") and p["body"]: # 


      author = "Anonymous" 
      # if the value for key "author" in p evaluates to True 
      # assign its value to the author variable. 
      if p["author"]: author = p["author"] 

      # create comment pointing to Post id: pk passed into this function 
      comment = Comment(post=Post.objects.get(pk=pk)) 

      # generate modelform to edit comment created above 
      cf = CommentForm(p, instance=comment) 
      cf.fields["author"].required = False 

      # use commit=False to return an unsaved comment instance 
      # presumably to add in the author when one hasn't been specified. 
      comment = cf.save(commit=False) 
      comment.author = author 
      comment.save() 
     return HttpResponseRedirect(reverse("dbe.blog.views.post", args=[pk])) 

著者は1が渡されていない場合は、著者のフィールドにデフォルト値を代入しようとしている。

おそらく、変更可能なコピーを作成することにより、コードをかなり短縮することができPOSTQueryDictの同じ問題を解決します。

これはあなたにとって理にかなっていますか?

class CommentForm(ModelForm): 
    class Meta: 
     model = Comment 
     exclude = ["post"] 

    def add_comment(request, pk): 
     """Add a new comment.""" 
     p = request.POST.copy() 

     if p.has_key("body") and p["body"]: 
      if not p["author"]: 
       p["author"] = 'Anonymous' 

      comment = Comment(post=Post.objects.get(pk=pk)) 
      cf = CommentForm(p, instance=comment) 
      cf.save() 

     return HttpResponseRedirect(reverse("dbe.blog.views.post", args=[pk])) 
+0

"POSTで" author "を指定した場合は、投稿された内容にvar authorを割り当てます。"間違っています。 –

+0

助けてくれてありがとう。 –

+0

@イグナチオ - 良い点!キー作成者の値が真であると評価するともっと似ていますか? –

0

これは、ユーザーが匿名として設定します著者名を入力しなかった場合、フォームを使用してコメントしているユーザは、フォーム(作者と本体) を充填しているかどうかをチェックし、(両方のフィールド場合著者と本文)が空の場合は、フォームにリダイレクトされます..

関連する問題