[Django]-Passing Data from post() to get_context_data()

1👍

You have to find a way to pass the object ID to the next page. The options that come to mind are to put it into the URL or as solarissmoke has suggested save it in the session. If you are doing it in the url you can also put the page sequence there (meaning 1 for the forst form, 2 for the second…).

The nice thing about this approach is, that you can cover all functonailty in one view: depending on the page set the respective fields in the get_object methods (self.fields=[….]) and the template names in the get_template_names method.

So using an Updateview, it would look like this:

urls.py:

....
url(r'^mysite/(?P<object_no>\d+)/(?P<form_no>\d+)$', BaseView.as_view()),

views.py:

class BaseView(UpdateView):
    def get_object(self):     
        obj=MyModel.objects.get(id=self.kwargs['object_no'])  
        form_no = self.kwargs['form_no']
        if form_no=="1":
             self_fields=["field1","field2"...]
        .....
    def get_object(self):     
        obj=MyModel.objects.get(id=self.kwargs['object_no'])  
        form_no = self.kwargs['form_no']
        if form_no=="1":
             self_fields=["field1","field2"...]  
        .....             
        return obj
    def get_template_names(self):
        from_no = self.kwargs['form_no']
        if form_no=="1":
             return ["template1.html"]
        ....

You have to make sure that all your fields can be null.

2👍

By defining post yourself, you’ve overridden the default behaviour of the view. You can see that there is no call to get_context_data, or any of the other class methods, so naturally they won’t be called.

Generally you should not be overriding the specific get or post methods. You haven’t shown the full view so it’s not clear what behaviour you are trying to achieve, but for example in a form view you would want to define success_url to set the place the form redirects to after submission.

Leave a comment