Difference between self.request and request in Django class-based view
<p>In django, for class-based view like <code>ListView</code> and <code>DetailView</code>, methods like <code>get()</code> or <code>post()</code> or other functions defined by developer take parameters include <code>self</code> and <code>request</code>. I learnt that in <code>self</code> these is actually a <code>self.request</code> field, so wha's the difference between <code>self.request</code> and <code>request</code>? </p>

<p>Example, this is the function in a class based view and used to handle user's login requirement:</p>

<pre><code>def login(self, request):
    name = request.POST['name']
    pwd = request.POST['password']
    user = authenticate(username=name, password=pwd)

    if user is not None:
        request.session.set_expiry(0)
        login(request, user)
        log_message = 'Login successfully.'
    else:
        log_message = 'Fail to login.'

    return HttpResponseRedirect(reverse('blog:testindex'))
</code></pre>

<p>This is the function used to handle user's register:</p>

<pre><code>def register(self, request):
    user_name = self.request.POST['username']
    firstname = self.request.POST['firstname']
    lastname = self.request.POST['lastname']
    pwd = self.request.POST['password']
    e_mail = self.request.POST['email']
    user = User.objects.create(username=user_name, first_name=firstname, last_name=lastname, email=e_mail)
    user.set_password(pwd)
    try:
        user.save()
        user = authenticate(username=user_name, password=pwd)
        login(self.request, user)
    except Exception:
        pass
    else:
        return HttpResponseRedirect(reverse('blog:testindex'))
</code></pre>

<p>In the first function, it used data stored in <code>request</code> and in the second one, it used <code>self.request</code>, both work functionally. What's the difference?</p>

<p>Thanks for your answers.</p>

<p>For a subclass of <code>View</code>, they're the same object. <code>self.request = request</code> is set in <code>view</code> function that <code>as_view()</code> returns. I looked into the history, but only found setting <code>self.request</code> and then immediately passing request into the view function.</p>

python django