配置 Django URLS.py 以在 URL 中以 / 重写后保留 #anchors
在我的 django 应用程序中,我将 URLS.PY 配置为接受对 /community/user/id 和 /community/user/id/ 的请求:
我这样做是因为有时人们会添加结尾“/”并且我不想引发 404。 但是我的 javascript 应用程序的某些部分有时会添加以下形式的锚标记: 我遇到的问题是 Django 会立即将 URL 重写为: 以 / 结尾并删除 #anchorIuseInJavscriptToDoSomething Id like将其重写为: 这样我的页面中的 JavaScript 仍然可以看到锚点并工作。如何调整这个正则表达式来做到这一点?我不太擅长正则表达式,并通过示例学习了这一点......url(r'^(?P
/community/user/id#anchorIuseInJavscriptToDoSomething
/community/user/id/
/community/user/id#anchorIuseInJavscriptToDoSomething/
我这样做是因为有时人们会添加结尾“/”并且我不想引发 404。
但是我的 javascript 应用程序的某些部分有时会添加以下形式的锚标记:
/community/user/id#anchorIuseInJavscriptToDoSomething
我遇到的问题是 Django 会立即将 URL 重写为:
/community/user/id/
以 / 结尾并删除 #anchorIuseInJavscriptToDoSomething
Id like将其重写为:
/community/user/id#anchorIuseInJavscriptToDoSomething/
这样我的页面中的 JavaScript 仍然可以看到锚点并工作。如何调整这个正则表达式来做到这一点?我不太擅长正则表达式,并通过示例学习了这一点......
In my django application I have my URLS.PY configured to accept requests to /community/user/id and /community/user/id/ with:
url(r'^(?P<username>[\w-]+)/(?P<cardId>\d+)/$', 'singleCard.views.singleCard', name='singleCardView'),
I did this as some times people will add an ending "/" and I didn't want to raise a 404.
However parts of my javascript application sometime add a anchor tag in the form of:
/community/user/id#anchorIuseInJavscriptToDoSomething
The problem I have is Django will instantly rewrite the URL to:
/community/user/id/
with an ending / and remove the #anchorIuseInJavscriptToDoSomething
Id like it to rewrite it to:
/community/user/id#anchorIuseInJavscriptToDoSomething/
This way my javascript in the page can still see the anchor and work. How can adapt this regex to do this? I'm not very good at regex, and learnt this one by example...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将尾部斜杠设为可选:
you could make the trailing slash optional:
浏览器应该在重定向后处理重新附加锚点。你的问题与Django无关。
The Browser should handle re-appending the anchor after the redirect. Your problem has nothing to do with Django.
为什么要将其更改为
/community/user/id#anchorIuseInJavscriptToDoSomething/
?这是无效的。它应该是/community/user/id/#anchorIuseInJavscriptToDoSomething
。哈希后的元素不是 URL 的一部分,不会发送到服务器。Why do you want to change it to
/community/user/id#anchorIuseInJavscriptToDoSomething/
? This is invalid. It should be/community/user/id/#anchorIuseInJavscriptToDoSomething
. The element after the hash is not part of the URL and is not sent to the server.