问题
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