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 l开发者_Python百科earnt this one by example...
you could make the trailing slash optional:
url(r'^(?P<username>[\w-]+)/(?P<cardId>\d+)/?$', 'singleCard.views.singleCard', name='singleCardView'),
The Browser should handle re-appending the anchor after the redirect. Your problem has nothing to do with Django.
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.
精彩评论