【问题标题】:Facing problem with user profile url scheme like example.com/username in django面临用户配置文件 url 方案的问题,如 django 中的 example.com/username
【发布时间】:2011-04-11 07:44:22
【问题描述】:

在 django 应用程序中,我需要创建具有以下结构的 twitter 用户个人资料网址:

  • example.com/<username>
  • example.com/<username>/friends
  • example.com/<username>/blog
  • example.com/<username>/some-page
  • example.com/<username>/some-other-page

我的 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'),
)

我的第一个问题是,虽然 example.com/&lt;username&gt; 工作正常,但 example.com/&lt;username&gt;/any-other-page 却不行。他们都以show_profile 视图结束,而不是他们自己的视图。

注意:如果我让 url 将 url 结构更改为 example.com/user/&lt;username&gt; ,一切正常

我在这里做错了什么?请指教。

其次,在处理第一部分本身是可变的 url 方案时,我想获得有关 django 最佳实践(陷阱、陷阱等)的指导。

谢谢

【问题讨论】:

  • 你的正则表达式中的0-0 很奇怪:我认为你的意思是0-9,虽然这不是你的问题。
  • 是的,我的代码中有错字。解决了这个问题。谢谢。

标签: django django-urls user-profile


【解决方案1】:

我不明白为什么您的网址不起作用。您可以尝试将第一个模式移到末尾,以便其他模式有机会首先匹配。您描述的问题听起来像example.com/user/any-page,模式匹配“用户/任何页面”作为帐户名称。您显示的正则表达式不会这样做,但您的实际代码可能略有不同?

【讨论】:

    猜你喜欢
    • 2015-04-16
    • 2011-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-18
    • 2011-07-13
    相关资源
    最近更新 更多