Inject template instead of success_url into form_valid with FormView django

as the title says: I need to render the template after the form is submitted, this form is processed by the FormView using the form_valid method. With the method post I can submit the afer submit template, but maybe with the form_valid I can do it in the cleanest way. How can i do this?

+3


source to share


1 answer


The default implementation is by default form_valid

to redirect to success_url

, you only need to override it to render some page. Here's an 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())

      

+9


source







All Articles