Yunti
Yunti

Reputation: 7428

Using sessions in a class based view

I'm trying to use sessions in a class based view (and have sessions set up as per the docs in apps and middleware). However I'm not sure if I should be doing this by overidding dispatch. With the below view I get the

'ServiceTypeView' object has no attribute 'method'

View:

class ServiceTypeView(CreateView):
    form_class = ServiceTypeForm
    template_name = "standard_form.html"
    success_url = '/'

    def dispatch(self, request, *args, **kwargs):
        request.session['dummy_data'] = 'initializer'
        return super().dispatch(self, request, *args, **kwargs)

(note using python 3)

Upvotes: 2

Views: 473

Answers (1)

Alasdair
Alasdair

Reputation: 308779

When you call the superclass' dispatch method, you shouldn't pass self explicitly. It should be:

return super().dispatch(request, *args, **kwargs)

Upvotes: 3

Related Questions