配置Django URLS.py以在末尾重写/后将#anchors保留在URL中 [英] Configure Django URLS.py to keep #anchors in URL after it rewrites it with a end /

查看:108
本文介绍了配置Django URLS.py以在末尾重写/后将#anchors保留在URL中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Django应用程序中,我将URLS.PY配置为接受对/ community / user / id和/ community / user / id /的请求,

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'),

我这样做是因为有时人们会添加一个结尾的 /,而我不想提出404

I did this as some times people will add an ending "/" and I didn't want to raise a 404.

不过我的javascript应用程序的某些部分有时会添加以下形式的锚标记:

However parts of my javascript application sometime add a anchor tag in the form of:

/community/user/id#anchorIuseInJavscriptToDoSomething

我遇到的问题是Django立即将URL重写为:

The problem I have is Django will instantly rewrite the URL to:

/community/user/id/ 

以/结尾,并删除#anchorIuseInJavscriptToDoSomething

with an ending / and remove the #anchorIuseInJavscriptToDoSomething

Id喜欢它重写为:

/community/user/id#anchorIuseInJavscriptToDoSomething/

这样我的JavaScript在页面中仍然可以看到锚点和工作。如何适应这个正则表达式来做到这一点?我不是很擅长正则表达式,并以示例的方式学习了它。

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...

推荐答案

您可以将斜杠设为可选:

you could make the trailing slash optional:

url(r'^(?P<username>[\w-]+)/(?P<cardId>\d+)/?$', 'singleCard.views.singleCard', name='singleCardView'),

这篇关于配置Django URLS.py以在末尾重写/后将#anchors保留在URL中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆