【问题标题】:How can I add an anchor to a URL when it has no anchors?当 URL 没有锚时,如何将锚添加到 URL?
【发布时间】:2014-11-12 18:49:28
【问题描述】:

当加载一个页面时,我希望用户被直接发送到它的一部分,称为#town-center。

我写了以下代码:

url(r'^(?i)(?P<town_slug>[-\w]+)/', RedirectView.as_view(permanent=False, url='#town-center')), # adds the "town-center" anchor to reposition the browser when displaying the town
url(r'^(?i)(?P<town_slug>[-\w]+)/#$', views.town_map, name='town_map'),

这适用于将#town-center 添加到当前 URL,但它失败了,因为第一个模式总是被拾取,并且它循环。

我从 Firefox 得到的确切错误是:“页面没有正确重定向”。

我怎样才能做到这一点?

【问题讨论】:

    标签: regex django anchor django-urls


    【解决方案1】:

    您无法在服务器端处理# 之后的url 部分。因为它只针对浏览器端,所以不会发送到服务器。

    因此在浏览器端使用 javascript 进行重定向。

    也看https://stackoverflow.com/a/3664324/821594

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 2023-03-17
      • 2014-11-14
      • 1970-01-01
      • 2010-12-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多