Render a template instead a success_url in form_valid with FormView django

China☆狼群 提交于 2019-12-10 14:47:15

问题


As the title says: I need to render a template after submitting a form, this form is handled with FormView with the method form_valid. With the method post, I can render a template after submitting it but maybe with form_valid, I can do it in the cleanest way.

How can I do it?


回答1:


The default implementation of form_valid is to redirect to success_url, you only need to override it to render some page. Here is the example.

class ChangePasswordPage(FormView):
    template_name = 'core/password-change.html'
    form_class = PasswordChangeForm

    def form_valid(self, form):
        form.save()
        messages.success(self.request, "Your password is changed")
        return render(self.request, 'core/password-change-success.html', self.get_context_data())


来源:https://stackoverflow.com/questions/30743759/render-a-template-instead-a-success-url-in-form-valid-with-formview-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!