Facing problem with user profile url scheme like example.com/username in django

百般思念 提交于 2019-12-19 10:41:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!