简体   繁体   English

登录后Django重定向

[英]Django redirect after login

Im creating my first app in Django I decided I want logging window (smal one with username/password na "Log in" button) on EACH view. 我在Django中创建了我的第一个应用程序,我决定要在每个视图上登录窗口(带有用户名/密码“登录”按钮的小窗口)。 From base.html: 从base.html:

<div id="logging">
    {% if user.is_authenticated %}
    <h1>Witaj, {{user.username}}!</h1>
    {% else %}
     <form method="post" action="{% url 'harcowanie.views.login' %}">
    {% csrf_token %}
    <div  style="display: inline-block;">
    <p><label for="id_username">Username:</label> <input id="id_username" type="text" name="username" maxlength="30" /></p>
    <p><label for="id_password">Password:</label> <input type="password" name="password" id="id_password" /></p>
    </div>
    <div style="display: inline-block; position: relative; bottom: 25px;">
    <input class="decent_background" type="submit" value="Log in" />
    </div>
</form>
{% endif %}
    </div>

Any part of my views.py: 我的views.py的任何部分:

def login(request):
    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=request, user=user)
            return redirect(request.META['HTTP_REFERER'],   context_instance=RequestContext(request))
        else:
            pass
            # Return a 'disabled account' error message
    else:
        pass
        # Return an 'invalid login' error message.

    return redirect(request.META['HTTP_REFERER'],   context_instance=RequestContext(request))

But I'm getting error: 但是我遇到了错误:

A {% csrf_token %} was used in a template, but the context did not provide the value. 在模板中使用了{%csrf_token%},但是上下文没有提供该值。 This is usually caused by not using RequestContext. 这通常是由于未使用RequestContext引起的。 warnings.warn("A {% csrf_token %} was used in a template, but the context did not provide the value. This is usually caused by not using RequestContext.") warnings.warn(“模板中使用了{{csrf_token%},但上下文未提供该值。这通常是由于未使用RequestContext引起的。”)

Looks like you use redirect in wrong way. 看起来您以错误的方式使用了redirect Argument context_instance is not supported by this method. 此方法不支持参数context_instance。 See docs . 参见docs

About error. 关于错误。 Ensure that view which rendered page from request.META['HTTP_REFERER'] uses RequestContext or manually import and use the processor to generate the CSRF token( docs ). 确保从request.META ['HTTP_REFERER']渲染页面的视图使用RequestContext或手动导入并使用处理器生成CSRF令牌( docs )。

The error states that the CSRF token is not in your context. 该错误表明CSRF令牌不在您的上下文中。 A good read to know what CSRF is about: here . 一本好书,了解CSRF的含义: 这里

In order to generate the CSRF token, either enable the middleware in your settings, or generate by hand in your view, then pass it to your template. 为了生成CSRF令牌,请在设置中启用中间件,或在视图中手动生成,然后将其传递给模板。 I strongly suggest to you to enable the middleware :) 我强烈建议您启用中间件:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM