Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render a template instead a success_url in form_valid with FormView django

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?

like image 201
dfrojas Avatar asked Jun 09 '15 22:06

dfrojas


1 Answers

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())
like image 54
Edwin Lunando Avatar answered Oct 24 '22 06:10

Edwin Lunando