django Next URL无法正常工作



我已经使用默认Django(1.9.5(auth_view和urls.py创建了一个登录页面,看起来像

url(r'^login/$', auth_views.login, {"template_name": 'login.html'}, name='login'),

并使用Django指定的HTML生成表单,而HTML看起来像

<form method="post" action="{% url 'login' %}?next={{next}}">
                {% csrf_token %}
                <table class="login-form">
                <tr>
                    <td class="input-label">{{ form.username.label_tag }}</td>
                    <td class="input-field"><input class="form-control" id="{{ form.username.id_for_label }}" maxlength="30" name="{{ form.username.html_name }}" type="text" /> 
                    </td>
                </tr>
                <tr>
                    <td class="input-label">{{ form.password.label_tag }}</td>
                    <td class="input-field"><input class="form-control" id="{{ form.password.id_for_label }}" maxlength="30" name="{{ form.password.html_name }}" type="password" /> 
                    </td>
                </tr>
                </table>
                {# Assumes you setup the password_reset view in your URLconf #}
                <p class="pull-left"><a href="{% url 'password_reset' %}">Forgot password?</a></p>
                <button type="submit" class="btn btn-default pull-right"/>
                    Sign in <span class="fa fa-arrow-circle-right"></span>
                </button>
                <input type="hidden" name="next" value="{{ next }}"/>
            </form>

现在,如果任何用户试图访问/admin之类的页面和用户未经身份验证,则用户被重定向到/登录,并且在成功登录后,他应根据用户请求将其重定向到/admin。但是我正在降落/登录/管理员。我要去哪里,

任何帮助将不胜感激。

不要在操作中使用查询参数,请设置表单的操作,例如

action="{% url 'login' %}"

您已经在代码中使用了<input type="hidden" name="next" value="{{ next }}"/>,该代码将处理URL的next

解决方案: 我发现了为什么它不起作用,在我的中间件中,我正在进行删除/admin之前的LSTRIP('/'(。当我尝试登录URL时,这意味着看起来像http://localhost:8000/login?/next = admin/而不是http://localhost:8000/login/?next =/admin/

最新更新