Python Django用户身份验证不工作/未登录

Python Django用户身份验证不工作/未登录,python,django,django-views,django-authentication,django-login,Python,Django,Django Views,Django Authentication,Django Login,我正在学习Tango with Django教程,并试图设置用户身份验证,但是我发现我的用户登录后无法在主页上成功进行身份验证(至少,我相信他们已登录) 我的用户登录视图是 def user_login(request): context = RequestContext(request) if request.method == 'POST': username = request.POST['username'] password = re

我正在学习Tango with Django教程,并试图设置用户身份验证,但是我发现我的用户登录后无法在主页上成功进行身份验证(至少,我相信他们已登录)

我的用户登录视图是

def user_login(request):
    context = RequestContext(request)

    if request.method == 'POST':

        username = request.POST['username']
        password = request.POST['password']

        user = authenticate(username=username, password=password)

        if user:
            if user.is_active:
                login(request, user)
                return HttpResponseRedirect('/')
            else:
                return HttpResponse("Your Rango account is disabled.")
        else:
            return HttpResponse("Invalid login details supplied.")
    else:
        return render(request, 'rango/user_login.html', {})
在我的主页模板中我有:

        {% if user.is_authenticated %}
        <h1>Rango says... hello {{ user.username }}!</h1>
        {% else %}
        <h1>Rango says... hello world!</h1>
        {% endif %}
{%if user.u经过身份验证%}
兰戈说。。。你好{{user.username}}!
{%else%}
兰戈说。。。你好,世界!
{%endif%}
成功登录后,它会将我带回主页并返回“HelloUSER NAME”,但它只会返回主页并显示“Hello World”,我不知道为什么

此外,如果有帮助,这是我的用户登录模板的主要部分,user_login.html:

    <form id="login_form" method="post" action="/login/">
        {% csrf_token %}
        Username: <input type="text" name="username" value="" size="50" />
        <br />
        Password: <input type="password" name="password" value="" size="50" />
        <br />
        <input type="submit" value="submit" />
    </form>

{%csrf_令牌%}
用户名:

密码:
我的调试表明用户正在登录,但用户信息没有传递到主页,特别是user变量为空。然而,我对Django还是相当陌生的,我自己也不知道到底要知道什么


有人知道我做错了什么吗

打印
用户名和密码的值,可能是
这就是他不工作的原因

信息:当您使用
请求时。POST['username']
如果您没有值,它将引发KeyError异常。我建议使用:

username = request.POST.get('username')
password = request.POST.get('password')
这样你就不会有错误,更多信息

要按自定义字段获取信息,请不要忘记向div/input添加
name
id

Username: <input type="text" name="username" value="" size="50" id="username" />
Password: <input type="password" name="password" value="" size="50" id="password" />
用户名:
密码:

打印
用户名
密码
的值,可能是
这就是他不工作的原因

信息:当您使用
请求时。POST['username']
如果您没有值,它将引发KeyError异常。我建议使用:

username = request.POST.get('username')
password = request.POST.get('password')
这样你就不会有错误,更多信息

要按自定义字段获取信息,请不要忘记向div/input添加
name
id

Username: <input type="text" name="username" value="" size="50" id="username" />
Password: <input type="password" name="password" value="" size="50" id="password" />
用户名:
密码:

丹尼尔·罗斯曼(Daniel Roseman)关于我的主页索引的评论,特别是render,解决了这个问题

我在用

return render_to_response('rango/index.html', context_dict, context)
但是,在我将其替换为

return render(request, 'rango/index.html', context_dict)
问题解决了


谢谢Daniel和其他所有做出贡献的人

丹尼尔·罗斯曼(Daniel Roseman)关于我的主页索引的评论,特别是render,解决了这个问题

我在用

return render_to_response('rango/index.html', context_dict, context)
但是,在我将其替换为

return render(request, 'rango/index.html', context_dict)
问题解决了


谢谢Daniel和其他所有做出贡献的人

您是否在设置中使用
auth.contextprocessor
?是否请求.user
在模板中工作?是否可以发布您的设置,特别是中间件和上下文处理器?请发布主页视图的代码。你确定你正在使用
render
吗?你在设置中使用了
auth.contextprocessor
吗?是否
请求.user
在模板中工作?你能发布你的设置,特别是中间件和上下文处理程序吗?请发布你主页视图的代码。您确定在那里使用的是
render
吗?
[]
语法有什么问题?没关系。区别只是
.get()
返回
None
如果没有区别,那么解释一下,
POST['xxx']
对我不起作用,但是
POST.get('xxx')
捕获数据或返回
None
。所以很容易发现问题,若你们并没有捕捉到数据,那个么登录就无法工作。所以问题不是登录而是捕获数据。
[]
语法有什么问题?没关系。区别只是
.get()
返回
None
如果没有区别,那么解释一下,
POST['xxx']
对我不起作用,但是
POST.get('xxx')
捕获数据或返回
None
。所以很容易发现问题,若你们并没有捕捉到数据,那个么登录就无法工作。因此,问题不是登录,而是捕获数据。