问题
In a django app, I need to create twitter user profile urls with following structure like:
example.com/<username>
example.com/<username>/friends
example.com/<username>/blog
example.com/<username>/some-page
example.com/<username>/some-other-page
My urls.py:
urlpatterns = patterns('profiles.views',
url(r'^(?P<account_name>[a-zA-Z0-0_.-]+)/$', 'show_profile', name='profiles_show_profile'),
url(r'^(?P<account_name>[a-zA-Z0-0_.-]+)/friends/$', 'show_friends', name='profiles_show_blog'),
url(r'^(?P<account_name>[a-zA-Z0-0_.-]+)/blog/$', 'show_blog', name='profiles_show_blog'),
)
My first problem is that while example.com/<username>
works fine example.com/<username>/any-other-page
does not. They all end up at show_profile
view instead of their own view.
Note: Everything works fine if I make urls change the url structure to example.com/user/<username>
What am I doing wrong here? Please advise.
Secondly, I would like guidance on django best practices(pitfalls, gotchas etc) in dealing with url schemes where first part is itself is variable.
Thanks
回答1:
I don't see why your URLs aren't working. You can try moving the first pattern to the end, so that other patterns have a chance to match first. The problem you're describing sounds like example.com/user/any-page
, the pattern is matching "user/any-page" as the account name. The regex you show wouldn't do that, but maybe your actual code is slightly different?
来源:https://stackoverflow.com/questions/3694795/facing-problem-with-user-profile-url-scheme-like-example-com-username-in-django