Urls.py unable to pass #(pound) character to a view in Django,

I used to pass data through django URL while passing #character is not able to pass through urls.py, I am using pattern as

url(r'^pass/(?P<sentence>[\w|\W]*)/$',pass) 

I tried with these pattern also

url(r'^pass/(?P<sentence>[a-zA-Z0-9-/:-?@#{-~!^_\'\[\]*]*)/$',pass) 

Thanks in advance.

Upvotes: 1

Views: 867

Answers (2)

Paulo Scardine
Paulo Scardine

Reputation: 77389

The "#" character marks inline anchors (links within the same page) in a URL, so the browser will never send it to Django.

For example, if the URL is /something/pass/#test/something-else/ the browser will sent only /something/pass/ to the server. You can try /something/pass/%23test/something-else/ instead, 23 is the hexadecimal ascii code for # - not pretty (ugly by ugly just pass it as a get variable instead).

There is nothing you can do on the Django side - you better avoid characters with special meanings in the URL path when designing your routes - of course it is a matter of taste, but I really think that strings passed in the URL path should be "slugfied" in order to remove any funny character.

Upvotes: 3

Leonardo.Z
Leonardo.Z

Reputation: 9801

Browsers won't send the url fragment part (ends with "#") to servers. Why not converting your data to base64 first, then pass the data via url.

RFC 1808 (Relative Uniform Resource Locators) : Note that the fragment identifier (and the "#" that precedes it) is not considered part of the URL. However, since it is commonly used within the same string context as a URL, a parser must be able to recognize the fragment when it is present and set it aside as part of the parsing process.

Upvotes: 0

Related Questions