我django.contrib.auth.views.login
在我的登录模板中使用这样的表单
<form method="post" action="/login/" id="loginform">{% csrf_token %}
<label for="id_username">Username:</label>
<br>
{{ form.username }}
<br>
<label for="id_password">Password:</label>
<br>
{{ form.password }}
<br>
<input type="hidden" name="next" value="/redirect/">
<input type='button' class='button' value='Sign In'>
</form>
我urls.py
是这样的:
url(r'^login/$', 'django.contrib.auth.views.login'),
url(r'^redirect/$', redirect)
我的观点是:
def redirect(request):
logging.debug('user logging in: %s' %request.user.username)
username = request.user.username
try:
user = User.objects.get(username=username)
[...]
问题是请求用户名是空的。所以我总是在user = User.objects.get(username=username)
任何想法是怎么发生的?
PS:管理员(django contrib admin)登录失败也是同样的原因
更新:在我的日志文件中,我得到了这个:
2013-06-17 07:11:01,932 DEBUG request user is: AnonymousUser
更新 2:
我还从 settings.py 中粘贴了这个:
MIDDLEWARE_CLASSES = (
'django.middleware.cache.UpdateCacheMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'main.common.tz_middleware.TimezoneMiddleware',
'main.common.sslMiddleware.SSLRedirect',
'django.middleware.cache.FetchFromCacheMiddleware',
)
更新3:事情变得越来越荒谬。我redirect
用这个替换了这个函数:
def redirect(request):
return HttpResponseRedirect('/home/')
它有效。因此,重定向时的请求对象会保留用户信息......但这不是解决方案