Python 如何解决django自定义用户登录错误:CSRF验证失败。请求中止

Python 如何解决django自定义用户登录错误:CSRF验证失败。请求中止,python,django,django-forms,django-templates,django-views,Python,Django,Django Forms,Django Templates,Django Views,我有一个django自定义用户登录表单,当用户登录时会显示以下错误 请帮忙 Forbidden (403) CSRF verification failed. Request aborted. Help Reason given for failure: CSRF token missing or incorrect. 以下错误显示在终端中 UserWarning: A {% csrf_token %} was use

我有一个django自定义用户登录表单,当用户登录时会显示以下错误 请帮忙

            Forbidden (403)
    CSRF verification failed. Request aborted.

    Help
    Reason given for failure:

        CSRF token missing or incorrect.
以下错误显示在终端中

    UserWarning: A {% csrf_token %} was used in a t
emplate, but the context did not provide the value.  This is usually caused by not using RequestContext.
  "A {% csrf_token %} was used in a template, but the context "
[08/May/2018 23:44:13] "GET /login/ HTTP/1.1" 200 7798
这是我的看法

@csrf\u保护
def用户登录(请求):
context=RequestContext(请求)
如有要求,请发送:
username=request.POST.get('username')
密码=request.POST.get('用户名')
用户=验证(用户名=用户名,密码=密码)
如果用户不是无:
如果user.u处于活动状态:
登录(请求、用户)
返回render_to_响应('app/index.html',context_实例=RequestContext(请求))
其他:
返回HttpResponse(“您的帐户已禁用。”)
其他:
返回render_to_响应('app/login.html',context_实例=RequestContext(请求))
其他:

返回render_to_response('app/login.html',{},context)
嘿,请检查您的视图。py我还没有看到您的代码,但我想您可能在没有RequestContext的情况下进行了渲染,这就是显示上述错误的原因。所以你最好使用

render(request, 'template_name', {params});
来自文档
render()与使用强制使用RequestContext的context_实例参数将_呈现给_response()的调用相同。

您的模板已受
{%csrf_token%}
保护,无需在视图中使用装饰程序: 另外,您可以使用
django.shortcuts中的
render
而不是使用render\u-to\u响应

我把你的代码减少了一点,这样更好用

from django.contrib import messages

def user_login(request):
    if request.POST:
        username = request.POST.get('username')
        password = request.POST.get('password')
        user = authenticate(username=username, password=password)
        if user:
            if user.is_active:
                login(request, user)
                return render(request,'app/index.html',{})
            # the else statement
            return HttpResponse("You're account is disabled.")
        # the else statement
        messages.add_message(request,messages.WARNING,'Credentials invalid')
    return render(request,'app/login.html', {})
在模板中,按如下方式检索消息:

{% if messages %}
<ul class="messages">
   {% for message in messages %}
   <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
   {% endfor %}
 </ul>
{% endif %}
{%if消息%}
    {消息%中的消息为%s} {{message}} {%endfor%}
{%endif%}
嗨,我用了这句话,可以吗?返回render_to_response('app/index.html',context_instance=RequestContext(request))我应该使用什么{params}')?您编写的代码很好,但render_to_response在2.0版之后已被弃用,因此您最好使用render()请检查您的settings.py以确保您的模板文件夹结构的地址,并确保“app/index.html”是正确的路径,{params}用于将变量传递给模板。例如:返回render(请求'app/index.html',{'title':'title_name'})感谢现在没有错误,但我无法登录,在终端中此错误显示,(凭证无效[09/May/2018 00:51:20]“POST/login/HTTP/1.1”200 7921),因为我的代码
print('凭证无效')
这是因为用户名或密码无效,我可以使用相同的用户名和密码登录到默认管理员,但在自定义登录表单中我不能。您的密码中有输入错误,
password=request.POST.get('password')
,而不是
request.POST.get('username')
非常感谢您现在一切正常,请再说一件事,如果用户名或密码无效,如何显示错误消息?