2016-03-21 30 views
2

我有一種方法將新的Comment插入到底座中,並且在這樣做之後,它會重定向回到之前的帖子,該帖子可以是任何。因此,要做到這一點,我做了如下規則:傳遞URL參數以呈現

return redirect(reverse('blog:post', args = (post_id,)))

有了它,讀取,通過傳遞到URL中的id的頁面被重定向回以前Post

現在的問題是萬一表格無效。我想顯示錯誤消息,但我認爲現在的方式是重新創建窗體,擦除任何消息。所以,在else的條件下,我想,而不是重定向,再次呈現並顯示消息。我已經做了這樣的:

return render(request, 'blog/post.html', post_id = post_id)

但後來,我需要找回相同的頁面我,無論id的參數。我需要像redirect函數那樣通過post_id,但我找不到方法。

這是整個方法:用於顯示Post,這取決於其id,由URL

def write_comment(request, post_id): 
    """ 
    Write a new comment to a post 
    """ 
    form = CommentForm(request.POST or None) 

    if form.is_valid(): 
     post = Post.objects.get(pk = post_id) 
     post.n_comments += 1 
     post.save() 

     comment = Comment() 
     comment.comment = request.POST['comment'] 
     comment.created_at = timezone.now() 
     comment.modified_at = timezone.now() 
     comment.post_id = post_id 
     comment.user_id = 2 
     comment.save() 

     return redirect(reverse('blog:post', args = (post_id,))) 
    else: 
     # Need to pass the parameter here, in order to not recreate the form 
     return render(request, 'blog/post.html') 

我的類視圖:

url(r'^post/(?P<id>[0-9]+)/$', views.GetPostView.as_view(), name = 'post'), 

GetPostView

class GetPostView(TemplateView): 
    """ 
    Render the view for a specific post and lists its comments 
    """ 
    template_name = 'blog/post.html' 

    def get(self, request, id): 
     return render(request, self.template_name, { 
      'post': Post.objects.get(pk = id), 
      'comments': Comment.objects.filter(post = id).order_by('-created_at'), 
      'form': CommentForm() 
    }) 

回答

1

您應該將表單作爲一個表單變量(上下文參數)。

render(request, 'blog/post.html', context={"form": form}) 

您可能還想重新組織東西。我將結合註釋處理邏輯與GetPostView。我會嘗試將您在get中的邏輯移動到get_context_data,這將在呈現get/post時使用。然後添加post方法(而不是write_comment,儘管您肯定可以從post中調用該方法)。在那裏,如果您需要按原樣呈現頁面,則只需嘗試調用post方法的super版本即可。

class GetPostView(TemplateView): 
    """ 
    Render the view for a specific post and lists its comments 
    """ 
    template_name = 'blog/post.html' 

    def get(self, request, id): 
     self.request = request 
     self.id = id 

     return super(GetPostView, self).get(request, self.id) 

    def post(self, request, id): 
     """ 
     Process comment 
     """ 
     self.form = CommentForm(request.POST or None) 

     if self.form.is_valid(): 
      post = Post.objects.get(pk = id) 
      post.n_comments += 1 
      post.save() 

      comment = Comment() 
      comment.comment = request.POST['comment'] 
      comment.created_at = timezone.now() 
      comment.modified_at = timezone.now() 
      comment.post_id = id 
      comment.user_id = 2 
      comment.save() 

      return redirect(reverse('blog:post', args = (id,))) 
     else: 
      return self.get(request, id) 

    def get_context_data(self, **kwargs): 
     form = self.form if hasattr(self, 'form') else CommentForm() 

     return { 
       'post': Post.objects.get(pk = self.id), 
       'comments': Comment.objects.filter(post = self.id).order_by('-created_at'), 
       'form': form 
     } 

聲明:我沒有運行此代碼,因此可能會出現一個愚蠢的錯誤。如果你指出,我會盡力清理它。

+0

差不多。該消息最終顯示,但我仍然需要接收'post_id'。該頁面是這個URL'/ post/ /',我需要在同一頁面上呈現頁面。 – mfgabriel92

+0

所以當你渲染表單驗證錯誤時,你也想要這篇文章?在這個方法中我看不到任何內容(除了重定向)。有沒有另一種方法呢? – bpscott

+0

是的,根據url中的id顯示帖子的類視圖。請看我編輯的問題。 – mfgabriel92