2006年09月20日 星期三 08:56
HI,
我在Django里使用了auth,但在模板文件里总是不能区分出用户是否登陆。下面是用于登陆的响应:
def login(request):
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
_login(request, user)
#if user is not None:
return HttpResponseRedirect('../')
这是模板文件里对应的内容:
{% if user.is_authenticated %}
Welcome, {{ user.username }}. Thanks for logging in.
登出
{% else %}
Welcome, new user. Please log in.
本来在login()之后,如果用户名和密码正确的话,就应该显示Thanks for logging in,但现在总是显示的new user。不知道为什么。我用了默认的account/login(模板用的也是上面这个),如果用我的login()正确输入用户名和密码后,再到这个页面,会看到登陆成功的提示。但我自己的login()转向之后不能,不知道为什么?
--------------
bruce.who.hk
2006-09-20
2006年09月20日 星期三 11:46
bruce.who.hk,您好!
先在头上加:from django.contrib.auth import authenticate, login
同时,确认系统的session已经可以工作了。相关的配置可以看一下doc里面的session部分。
下面是我写blog时候的一段login代码,确认是可以工作的:
==========
def do_login(request):
if request.user.is_authenticated():
return HttpResponseRedirect("/app/mblog/")
if request.POST.has_key('username') and request.POST.has_key('password'):
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
login(request, user)
return HttpResponseRedirect("/app/mblog/")
else:
return HttpResponseRedirect("/app/mblog/login/")
==========
======= 2006-09-20 08:56:55 您在来信中写道:=======
>HI,
>
>我在Django里使用了auth,但在模板文件里总是不能区分出用户是否登陆。下面是用于登陆的响应:
>
>def login(request):
> username = request.POST['username']
> password = request.POST['password']
> user = authenticate(username=username, password=password)
> if user is not None:
> _login(request, user)
> #if user is not None:
> return HttpResponseRedirect('../')
>
>这是模板文件里对应的内容:
>
>{% if user.is_authenticated %}
> Welcome, {{ user.username }}. Thanks for logging in.
> 登出
>{% else %}
> Welcome, new user. Please log in.
>
>本来在login()之后,如果用户名和密码正确的话,就应该显示Thanks for logging in,但现在总是显示的new user。不知道为什么。我用了默认的account/login(模板用的也是上面这个),如果用我的login()正确输入用户名和密码后,再到这个页面,会看到登陆成功的提示。但我自己的login()转向之后不能,不知道为什么?
>
>--------------
>bruce.who.hk
>2006-09-20
>_______________________________________________
>python-chinese
>Post: send python-chinese在lists.python.cn
>Subscribe: send subscribe to python-chinese-request在lists.python.cn
>Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn
>Detail Info: http://python.cn/mailman/listinfo/python-chinese
= = = = = = = = = = = = = = = = = = = =
致
礼!
charles huang
hyy在fjii.com
TEL: 0591-3333169-212
2006-09-20
2006年09月20日 星期三 13:38
Hi, charles huang
找到原因了,如果在模板文件中要使用user的话,必须要传给它一个context_instance参数:
return render_to_response('weeklog/index.html',
context_instance=RequestContext(request))
-------------------------------------------------------------
> 发件人:charles huang
> 发送日期:2006-09-20 11:47:27
> 主题:Re: [python-chinese]为什么Django里的认证不起作用?
>bruce.who.hk,您好!
>
> 先在头上加:from django.contrib.auth import authenticate, login
> 同时,确认系统的session已经可以工作了。相关的配置可以看一下doc里面的session部分。
>
>下面是我写blog时候的一段login代码,确认是可以工作的:
>==========
>def do_login(request):
> if request.user.is_authenticated():
> return HttpResponseRedirect("/app/mblog/")
>
> if request.POST.has_key('username') and request.POST.has_key('password'):
> username = request.POST['username']
> password = request.POST['password']
> user = authenticate(username=username, password=password)
> if user is not None:
> login(request, user)
> return HttpResponseRedirect("/app/mblog/")
> else:
> return HttpResponseRedirect("/app/mblog/login/")
>==========
>
>======= 2006-09-20 08:56:55 您在来信中写道:=======
>
>>HI,
>>
>>我在Django里使用了auth,但在模板文件里总是不能区分出用户是否登陆。下面是用于登陆的响应:
>>
>>def login(request):
>> username = request.POST['username']
>> password = request.POST['password']
>> user = authenticate(username=username, password=password)
>> if user is not None:
>> _login(request, user)
>> #if user is not None:
>> return HttpResponseRedirect('../')
>>
>>这是模板文件里对应的内容:
>>
>>{% if user.is_authenticated %}
>> Welcome, {{ user.username }}. Thanks for logging in.
>> 登出
>>{% else %}
>> Welcome, new user. Please log in.
>>
>>本来在login()之后,如果用户名和密码正确的话,就应该显示Thanks for logging in,但现在总是显示的new user。不知道为什么。我用了默认的account/login(模板用的也是上面这个),如果用我的login()正确输入用户名和密码后,再到这个页面,会看到登陆成功的提示。但我自己的login()转向之后不能,不知道为什么?
>>
>>--------------
>>bruce.who.hk
>>2006-09-20
>>_______________________________________________
>>python-chinese
>>Post: send python-chinese在lists.python.cn
>>Subscribe: send subscribe to python-chinese-request在lists.python.cn
>>Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn
>>Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>= = = = = = = = = = = = = = = = = = = =
>
>
> 致
>礼!
>
>
> charles huang
> hyy在fjii.com
> TEL: 0591-3333169-212
> 2006-09-20
>
>_______________________________________________
>python-chinese
>Post: send python-chinese在lists.python.cn
>Subscribe: send subscribe to python-chinese-request在lists.python.cn
>Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn
>Detail Info: http://python.cn/mailman/listinfo/python-chinese
------------------
bruce.who.hk
2006-09-20
Zeuux © 2025
京ICP备05028076号