title: Passing URL parameters to render
body: <p>I have a method that inserts a new <code>Comment</code> into the base, and after doing so, it redirects back to the previous post, which can be any. So, to do that, I made the following rule:</p>
  
  <blockquote>
    <p><code>return redirect(reverse('blog:post', args = (post_id,)))</code></p>
  </blockquote>
  
  <p>With it, the page is redirected back to the previous <code>Post</code> being read, by passing to the URL the <code>id</code>.</p>
  
  <p>The problem is now in case the form is not valid. I want to display error messages, but I think that the way it is now, the form is being recreated, erasing any message. So, in the <code>else</code> condition, I want to, instead of redirecting, rendering it again and showing the messages. I've done it like this:</p>
  
  <blockquote>
    <p><code>return render(request, 'blog/post.html', post_id = post_id)</code></p>
  </blockquote>
  
  <p>But then, I need to get back to the same page I was, regardless the <code>id</code> in the parameter. I need to pass the <code>post_id</code> as I did in the <code>redirect</code> function, but I can't find a way.</p>
  
  <p>This is the whole method:</p>
  
  <pre><code>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')
  </code></pre>
  
  <p>My class view used to display the <code>Post</code>, depending on its <code>id</code>, by the URL:</p>
  
  <pre><code>url(r'^post/(?P&lt;id&gt;[0-9]+)/$', views.GetPostView.as_view(), name = 'post'),
  </code></pre>
  
  <p>And the <code>GetPostView</code>:</p>
  
  <pre><code>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()
      })
  </code></pre>