Access request object from class based view

django, python

Solution

As mentioned by @karthikr you shouldn't be overriding `__init__()`. The request object is first available in the `dispatch()` method, which is called immediately after `__init__()`, but you shouldn't need to override that method either. Its primary purpose is to call the `get()`, `post()`, or other relevant method handlers. Generally speaking, however, it's not necessary to override those either.

If you really absolutely must catch the request at the earliest point possible though, then the dispatch method is your best bet.

class LoginView(TemplateView):
    def dispatch(self, request, *args, **kwargs):
        print self.request  # Works!
        return super(LoginView, self).dispatch(request, *args, **kwargs)  # Don't forget this

Problem

I have code like below ``` url(r'login$', views.LoginView.as_view(), name='login'), ``` and view as follows ``` class LoginView(TemplateView): def __init__(self, *args, **kwargs): #How to operate on request Object's type and its params. ``` I have mentioned my question as comment in code.

Original source