【问题标题】:django-registration and redirect containing hashtag包含主题标签的 django-registration 和重定向
【发布时间】:2013-09-05 08:05:26
【问题描述】:

我正在使用 django-registration 在我的 Django 应用程序中处理登录和用户注册。我希望用户在成功登录系统后被重定向到他们请求的 url(假设他们在最初尝试访问该 url 时已注销)。

登录网址如下所示:

http://localhost:8000/accounts/login/?next=/#foo/bar/

但是,在登录后,用户总是以http://localhost:8000/ 结束。我已经用http://localhost:8000/accounts/login/?next=/abc/def/ 试过了,效果很好,所以我认为主题标签是问题所在。如何解决这个问题(如果有的话)?

更新

我应该提到它是一个单页应用程序,因此标签...

【问题讨论】:

  • 你想做什么?主题标签旨在引用页面的特定部分。为什么localhost:8000/#foo/bar 有意义?
  • 这是一个单页应用程序。我想将用户重定向到上次访问的页面。
  • 我一直在试图找到解析“next”参数的代码,但它看起来不像 django-registration 或 Django 核心的代码吗?抱歉,如果我没有提供帮助。
  • 感谢您的调查。我似乎很奇怪,这不起作用。一个简单的(强制)redirect('#foo/bar/') 工作正常。

标签: python django django-registration


【解决方案1】:

我开始单步执行 django-registration(实际上是 django)代码。我在登录期间找不到任何 django-registration 正在做的事情,所以我认为它甚至不负责这部分。

Django 将 @login_required 视图函数包装在 _wrapped_view(request, *args, **kwargs) 函数 (django.contrib.auth.decorators) 中,并在执行实际函数之前执行身份验证测试。如果发现尚未登录,则获取当前 url 为 next (path = request.build_absolute_uri()) 并开始登录。但是,request.build_absolute_uri() 甚至不返回主题标签 URL。提取url的#-appended部分一般看起来不是possible

【讨论】:

    【解决方案2】:

    对不起,死灵法师,但我用以下方法解决了这个问题:

    我有一个登录表单模板,其中包含以下内容:

    <form method="post" action="{% url 'login' %}">
        {% csrf_token %}
        <input type="hidden" id="hidden_next" name="next" value="{{ next }}">
        ... the actual form here ...
    </form>
    

    最后(我使用的是 JQuery)下面的脚本:

    <script>$(function () { 
        $("#hidden_next").attr(
            "value", "{{ next }}" + 
                $(location).attr('hash'))
        });
    </script>
    

    请注意,我重复 {{ next }} 以防万一用户禁用 Javascript。在单页应用中,这不是问题,因为禁用 javascript 的用户根本无法使用该页面。

    【讨论】:

      猜你喜欢
      • 2017-05-08
      • 2012-07-05
      • 1970-01-01
      • 1970-01-01
      • 2012-12-22
      • 2023-04-05
      • 2011-01-23
      • 1970-01-01
      • 2012-05-06
      相关资源
      最近更新 更多