gpt4 book ai didi

django - 传递 URL 参数进行渲染

转载 作者:行者123 更新时间:2023-12-04 18:00:32 25 4
gpt4 key购买 nike

我有一个方法可以插入一个新的 Comment进入基地,这样做之后,它重定向回上一个帖子,可以是任何帖子。因此,为此,我制定了以下规则:

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



有了它,页面被重定向回之前的 Post被读取,通过传递给 URL 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')

我的类 View 曾经显示 Post ,取决于其 id ,通过网址:
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()
})

最佳答案

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

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

您可能还想重新组织事物。我会将评论处理逻辑与 GetPostView 结合起来.我会尝试移动您在 get 中的逻辑转至 get_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
}

免责声明:我没有运行这段代码,所以很可能是一个愚蠢的错误。如果你指出它们,我会尽力清理它。

关于django - 传递 URL 参数进行渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36122858/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com