gpt4 book ai didi

python - 无法在基本模板中使用 user.username

转载 作者:行者123 更新时间:2023-12-01 05:32:25 26 4
gpt4 key购买 nike

嗨,我尝试在我的基本模板中使用用户对象,但即使在登录页面后,用户对象每次也返回空值

请在这里找到我的代码:

浏览次数:

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 对象。

关于python - 无法在基本模板中使用 user.username,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19911143/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com