Render a template instead a success_url in form_valid with FormView django

6.4k views Asked by At

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

There are 1 answers

2
Edwin Lunando On BEST ANSWER

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