Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass context data in success_url?

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!

like image 657
JavaCake Avatar asked Sep 30 '22 14:09

JavaCake


1 Answers

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.

like image 174
Hasan Ramezani Avatar answered Oct 03 '22 03:10

Hasan Ramezani