【问题标题】:django next url not working correctlydjango 下一个 url 无法正常工作
【发布时间】:2017-09-12 23:28:59
【问题描述】:

我使用默认的 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 之类的页面并且用户未通过身份验证,则用户将被重定向到 /login,并且在成功登录后,他应该根据用户请求被重定向到 /admin。但我改为登陆 /login/admin 。我哪里错了,

感谢任何帮助。

【问题讨论】:

  • 试试action="."action="{% url 'login' %}"
  • 我没有写任何登录视图,视图来自 django auth_view。我不知道那里是如何处理重定向的

标签: python django next


【解决方案1】:

解决方案: 我找到了为什么它不起作用,在我的中间件中,我正在执行 lstrip('/') ,它在管理员之前删除 / 。这意味着当我尝试登录时,url 看起来像 http://localhost:8000/login?/next=admin/ 而不是 http://localhost:8000/login/?next=/admin/

【讨论】:

    【解决方案2】:

    不要在动作中使用查询参数,将你的动作设置为像

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

    您已经在代码中使用了&lt;input type="hidden" name="next" value="{{ next }}"/&gt;,它将处理您网址的next

    【讨论】:

      猜你喜欢
      • 2019-02-15
      • 1970-01-01
      • 2013-10-21
      • 2014-03-07
      • 2013-02-25
      • 2015-01-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多