Django URL parameters from external apps
I am using Django and I have the following URL in my project's urls.py file.
(r'^user/(?P<username>[\w_\-\.]+)/my_app/', include('my_app.urls')),
(r'^user/(?P<username>[\w_\-\.]+)/my_other_app/', include('my_other_app.urls')),
...
The goal is to have an application that uses the username of a user e.g. a profile application where every user has a profile page. Only one view in this application needs to have the username in the URL (the one that renders the profile page), but all the views must take a username
parameter even if they don't do anything with it.
I suspect that this usage of URL parameters is wrong because it forces ev开发者_运维技巧ery view of my_app
to take username as a parameter (because it's passed as from the URL dispatcher). To me it doesn't make sense for an external component (the project's urls.py file) to tell my_app
's URLs what parameters to take.
Is this usage correct? If not, how should I do this?
It does seem kind of odd.
Could you not switch your apps to look for the username parameter instead? Under my_app.urls
and the latter, just have the username
field in the lookup on the fields you need.
That way your structure looks more like:
/user/my_app/friends/hekevintran/
/user/my_other_app/connections/bartek/
Which works just as well and doesn't force your apps to rely on the context of your root project for the username.
精彩评论