在 django 应用程序中,我需要创建具有以下结构的 twitter 用户个人资料 url,例如:
example.com/<username>
example.com/<username>/friends
example.com/<username>/blog
example.com/<username>/some-page
example.com/<username>/some-other-page
我的网址.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/<username>
工作正常example.com/<username>/any-other-page
才不是。他们都最终在show_profile
视图而不是他们自己的视图。
Note:如果我将 urls 的 url 结构更改为example.com/user/<username>
我在这里做错了什么?请指教。
其次,我想要有关 django 最佳实践(陷阱、陷阱等)的指导,以处理第一部分本身是可变的 url 方案。
Thanks
我不明白为什么你的网址不起作用。您可以尝试将第一个模式移到末尾,以便其他模式有机会先匹配。你描述的问题听起来像example.com/user/any-page
,该模式匹配“user/any-page”作为帐户名。您显示的正则表达式不会这样做,但也许您的实际代码略有不同?
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)