【发布时间】:2017-08-29 12:13:01
【问题描述】:
我是django 的新手。我正在我的应用程序中实现用户login 机制。当用户登录时,我可以通过打开管理应用程序来验证用户是否已登录并且用户确实登录了。现在,当我尝试给用户一条消息时,例如 {{ Hello user.username }} 我什么也没得到。以下是我的完整代码。
view.py
def login_user(request):
context = RequestContext(request)
if request.method == "POST":
login_username = request.POST['username']
login_password = request.POST['password']
user = authenticate(username=login_username,password=login_password)
if user is not None:
if user.is_active:
login(request,user)
return HttpResponseRedirect('/albums/')
else:
print "Your Account is disabled"
else:
print "Invalid login details: {0}, {1}".format(login_username, login_password)
return HttpResponse("Invalid login details supplied.")
else:
return render(request,'music/login.html',{},context)
albums.html
{% if user.is_authenticated %}
<h1>hello {{ user.username }}!</h1>
{% else %}
<h1>hello world!</h1>
{% endif %}
帮帮我,我正在使用 Django 1.10 版和 python 2.7
【问题讨论】:
-
哪个视图使用
index.html模板?因为上面的代码只是显示登录后页面重定向到/albums/。你也没有收到hello world!吗? -
对不起,我刚刚编辑了。不是 index.html,而是 album.html。
标签: django python-2.7 django-views