Configure Django URLS.py to keep #anchors in URL after it rewrites it with a end /

一曲冷凌霜 提交于 2020-01-24 12:47:16

问题


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


回答1:


you could make the trailing slash optional:

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



回答2:


The Browser should handle re-appending the anchor after the redirect. Your problem has nothing to do with Django.




回答3:


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.



来源:https://stackoverflow.com/questions/3367194/configure-django-urls-py-to-keep-anchors-in-url-after-it-rewrites-it-with-a-end

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!