Search code examples
djangodjango-viewsdjango-urlsdjango-formwizard

accessing request object in a SessionWizardView in django


I am using a django forms SessionWizardView to implement a form wizard. The url configuration is as follows;

url(r'^contact/(?P<pk>\d+)/$', views.ContactWizard.as_view([DummyForm, OtherForm]), name='contact'),

The idea here is that I am passing a primary key so that I can populate the form with some initial data. The form is loaded from another view as follows:

def populate_review_form(request):
    pk = 2
    return redirect('contact', pk=pk)

Here I am trying to pass a hard-coded primary key to the form.

Now, my form implementation is simple where I am overriding the get_form_initial to generate the initial value for the form as follows:

class ContactWizard(SessionWizardView):
    def get_template_names(self):
        return "test.html"

    def get_form_initial(self, step):
        pk = self.request.GET.get('pk')
        # Just attempt to print the primary key
        print pk, self.request.GET
        return {}    

    def done(self, form_list, **kwargs):
        return HttpResponseRedirect('index')

The issue is that this always prints None <QueryDict: {}>. So, basically my parameter is not being passed or I am unable to access the correct request object. Is it possible in Django to have this communication?


Solution

  • pk is stored in self.kwargs:

    pk = self.kwargs['pk']