I have made a form where i wish to return to the same form again, this time with context data that can be used in my template to show that the form has been successfully sent.
How can i do this?
class ContactUsView(FormView):
form_class = ContactUsForm
template_name = 'website/pages/contact_us.html'
def form_valid(self, form):
form.send_email()
return super(ContactUsView, self).form_valid(form)
def get_success_url(self):
# Something here?
So basically i want get_success_url
to return to the ContactUsView
with e.g. {'success':'true'}
which i can read in the template and render a box that says it has been successfull. I dont want to change to another static page!
add a url like this to urls.py
:
url(r'^contact/(?P<success>\w+)$', ContactUsView.as_view(), name="ContactUsView"),
And you can access to this parameter in class-based
view like this:
add get_context_data
method to your class-view
class ContactUsView(DetailView):
context_object_name = 'my_var'
def get_context_data(self, **kwargs):
context = super(ContactUsView, self).get_context_data(**kwargs)
context['success'] = self.success
return context
And you can use {{ my_var.success }}
in your template.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With