i am experimenting with UTF-8 for usernames in a django app.
Django is Version 3.2.11
Python used is 3.9.4
Some users might have a profile visible to others and ther username in the url:
re_path("^u/(?P<username>\w )/$", views.author_profile_view, name="author_profile_view"),
Normal Example works fine:
- Browser shows ->
/u/brainyChowder3/ - Django shows ->
GET /u/brainyChowder3/ HTTP/1.1" 200 10593
UTF-8 example 1 works also fine:
- Browser shows ->
/u/ɊȁⱲÒđΈⱦİĬd/ - Django shows ->
GET /u/ɊȁⱲÒđΈⱦİĬd/ HTTP/1.1" 200 12508
But this UTF-8 does not work:
- Browser shows ->
/u/ɂáⱳ1⁄4|ĭğę - Django shows ->
"GET /u/ɂáⱳ1⁄4|ĭğę HTTP/1.1" 404 5585
The browser does show it strange, as he does not "translate" | to |, but that should be just optical?
Error shown is just
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/u/ɂáⱳ1⁄4|ĭğę
The current path, u/ɂáⱳ1⁄4|ĭğę, didn’t match any of these.
In Django shell I can query this user:
>>> User.objects.get(username='ɂáⱳ1⁄4|ĭğę')
<User: ɂáⱳ1⁄4|ĭğę>
The URI decoding looks ok to me.
I hope someone can explain why this is happening to one UTF-8 string, but not the other. Or maybe even knows how to fix it? :-D
I know it may not be the smartes thing to allow all UTF-8 for usernames, but this is more an experiment for me.
Thanks
CodePudding user response:
The reason this happens has nothing to do with UTF-8, but with the fact that the username contains a non-word character (a character not matched by \w): a character that is not allowed for the <str:…> path converter. You can work with a <path:…>:
path('u/<path:username>/', some_view, name='some_name') 