[英]Do I need to create a completely new view for my success page?
I have a form, and when the user submits valid information (clicking the 'submit' button on the page), I currently have a redirect to a success page occur. 我有一个表单,当用户提交有效信息(点击页面上的“提交”按钮)时,我目前正在重定向到成功页面。
class PostAdPage(CreateView):
template_name = 'post_ad.html'
success_url = '/awesome/'
form_class = PostAdForm
def form_valid(self, form):
form.save()
return HttpResponse("Awesome.")
I don't want the redirect to occur. 我不希望重定向发生。 Instead, I want the HTTP response to be displayed in place of the form. 相反,我希望显示HTTP响应来代替表单。
Without creating another view to render the response to the submission, is there a way to display the response? 如果不创建另一个视图来呈现提交的响应,有没有办法显示响应?
Yes, 是,
It's better and a safe practice to redirect to a new page after a successful action. 成功操作后重定向到新页面更好,更安全。
If you render your response on the POST method, the user can refresh the page and the form will be send it again. 如果您在POST方法上呈现响应,则用户可以刷新页面,表单将再次发送。 If you redirect to another page you will not have that problem. 如果您重定向到另一个页面,则不会出现此问题。
See http://en.wikipedia.org/wiki/Post/Redirect/Get for a more detailed argument. 有关更详细的参数,请参见http://en.wikipedia.org/wiki/Post/Redirect/Get 。
Use JavaScript instead. 请改用JavaScript。
As an alternative / a more global solution, you could consider using the messages framework . 作为替代/更全面的解决方案,您可以考虑使用消息框架 。 It is really convenient to use, and make you avoid some extra views. 使用起来非常方便,并且可以避免一些额外的视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.