传递 URL 参数进行渲染

5 django parameters url

我有一个方法可以将新内容插入Comment到基础中,执行此操作后,它会重定向回上一篇文章,可以是任何内容。因此,为了做到这一点,我制定了以下规则:

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

有了它,Post通过将id.

现在的问题是表格无效。我想显示错误消息,但我认为按照现在的方式,正在重新创建表单,删除任何消息。因此,在这种else情况下,我想再次渲染并显示消息,而不是重定向。我是这样做的:

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

id但是,无论参数如何,我都需要返回到原来的页面。我需要post_id像在redirect函数中那样传递,但我找不到方法。

这是整个方法:

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')
Run Code Online (Sandbox Code Playgroud)

我的类视图用于根据 URL显示Post,具体取决于其:id

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

还有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()
    })
Run Code Online (Sandbox Code Playgroud)

bps*_*ott -1

您应该将表单作为上下文变量(上下文参数)传递。

render(request, 'blog/post.html', context={"form": form})
Run Code Online (Sandbox Code Playgroud)

您可能还想重新组织事情。我会将评论处理逻辑与GetPostView. 我会尝试将您的逻辑移至getget_context_data这将在渲染 get/post 时使用。然后添加一个post方法(而不是write_comment,尽管您当然可以从 调用它post)。在那里,如果您需要按原样呈现页面,您只需尝试调用super该方法的版本即可post

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
        }
Run Code Online (Sandbox Code Playgroud)

免责声明:我没有运行此代码,因此可能会犯一个愚蠢的错误。如果你指出来,我会尽力清理它。