嗨,我正在尝试在我的基本模板中使用用户对象,但是即使登录到页面后,用户对象每次仍返回null值
请在这里找到我的代码:
观看次数:
def loginView(request):
if request.POST:
username = request.POST['username']
password = request.POST['password']
user = authenticate(username =username, password = password)
if user is not None:
if user.is_active:
login(request, user)
return HttpResponseRedirect('/rango/home/')
else:
print 'No user exist'
else:
c= {}
c.update(csrf(request))
return render_to_response('login.html', c)
基本模板
<diV style="float: left;">
{% block sidebar %}
<div>
{% if user.is_authenticated %}
<a href="{%url 'addCategory' %}">Add Category</a></br>
<a href="{%url 'logout' %}">Logout</a>
{% endif %}
<a href="{%url 'register'%}">Register</a></br>
<a href="{%url 'login'%}">Login</a></br>
<a href="{%url 'about' %}" >About</a></br>
</div>
{% endblock %}
</diV>
将您的代码更改为
return render_to_response('login.html', c,
context_instance=RequestContext(request))
如果没有context_instance
django,则不会在模板上下文中添加默认上下文处理器数据。因此,您不会user
在模板中得到对象。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句