简体   繁体   English

我是否需要为我的成功页面创建一个全新的视图?

[英]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。

  • Users fill in the form. 用户填写表格。
  • Using JavaScript, you perform a POST of the form's contents to a URL and the Django view returns success or failure after the form's contents have been processed. 使用JavaScript,您可以将表单内容的POST执行到URL,并且Django视图在表单的内容处理完毕后返回成功或失败。 Instead of returning HTML the view could return a JSON response with any information that you need (eg, an error message). 视图可以返回带有您需要的任何信息的JSON响应(例如,错误消息),而不是返回HTML。
  • If it failed, you can display the error message(s). 如果失败,您可以显示错误消息。
  • If it was successful, you can remove the form and replace the HTML of the form with a success message. 如果成功,您可以删除表单并用成功消息替换表单的HTML。

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.

相关问题 我在 Django 视图中的 success_url 中放了什么。 现在,这不是保存编辑过的表单,也不是重定向页面 - What do I put in the success_url in my django view. Right now, this isn't saving the edited form nor redirecting the page 我希望在同一页面上创建视图和列表视图 - i want my create view and list view on the same page 如何在类视图的 success_url 中包含 get_absolute_url - how do i include get_absolute_url in my success_url of a class view 我需要为我的新域购买 SSL 证书吗? - Do I need to buy an SSL certificate for my new domain? 我是否需要为Django中的表单创建forms.py? - Do I need to create forms.py for my forms in Django? 如何获取上下文来测试我的视图页面? - How do i get the context to test my view page? 成功消息不适用于创建视图 - success message is not working with create view 在Django应用程序中创建新用户时是否需要使用set_unusable_password()? - Do I need to use set_unusable_password() when creating new users in my Django application? 我是否需要为新的virtualenv重新安装Django? - Do I need to reinstall Django for new virtualenv? 为什么我需要为公共Django应用程序创建虚拟环境? - Why do I need to create a virtual environment for my public Django application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM