使用FormView django在form_valid中渲染模板而不是success_url [英] Render a template instead a success_url in form_valid with FormView django

查看:102
本文介绍了使用FormView django在form_valid中渲染模板而不是success_url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所述:我需要在提交表单后呈现模板,该表单是通过 FormView 和方法 form_valid 处理的。 code>。使用方法 post ,我可以在提交模板后呈现模板,但是也许可以使用 form_valid 在模板中进行渲染

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.

我该怎么办?

推荐答案

form_valid 的默认实现是重定向到 success_url ,您只需要重写它即可呈现某些页面。这是示例。

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())

这篇关于使用FormView django在form_valid中渲染模板而不是success_url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆