擅长:python、mysql、java
<p>在<code>AuthenticationMiddleware</code>里。你知道吗</p>
<p>官方文件提到:</p>
<p><a href="https://docs.djangoproject.com/en/dev/topics/auth/#installation" rel="nofollow">link</a>:</p>
<blockquote>
<p>AuthenticationMiddleware associates users with requests using
sessions. </p>
</blockquote>
<p><a href="https://docs.djangoproject.com/en/dev/ref/middleware/#django.contrib.auth.middleware.AuthenticationMiddleware" rel="nofollow">link</a>:</p>
<blockquote>
<p>class AuthenticationMiddleware</p>
<p>Adds the user attribute, representing
the currently-logged-in user, to every incoming HttpRequest object.
See Authentication in Web requests.</p>
</blockquote>
<p>源代码(django.contrib.auth公司.中间件.py)地址:</p>
<pre><code>class AuthenticationMiddleware(object):
def process_request(self, request):
assert hasattr(request, 'session'), "The Django authentication middleware requires session middleware to be installed. Edit your MIDDLEWARE_CLASSES setting to insert 'django.contrib.sessions.middleware.SessionMiddleware'."
request.user = SimpleLazyObject(lambda: get_user(request))
</code></pre>