Django:如何在urls.py中添加同一文件中的其他网址模式?

时间:2014-11-14 10:40:06

标签: django django-urls

我已经看过文档here,我正在尝试做同样的事情,但它不起作用。它与网址不匹配。

这是我的urls.py

profile_patterns = patterns('',
    url(r'^profile/$', views.profile),
    url(r'^thoughts/$', views.thoughts),
)

urlpatterns = patterns('',      
    url(r'^/user/(?P<username>\S+)/', include(profile_patterns)),

    # I also tried to do it this way.. But it isn't working.
    url(r'^abc/', include(patterns('', 
        url(r'^hello/$', views.profile)))),

我尝试访问以下网址。

'http://<mysite>.com/user/<someUsername>/profile/'
'http://<mysite>.com/abc/hello/'

1 个答案:

答案 0 :(得分:2)

试试这个\w+代替\S+而不是'',但是请看用户观看的路径:

profile_patterns = patterns('userapp.views',
  url(r'^profile/$', profile),
  url(r'^thoughts/$', thoughts),
) 

urlpatterns = patterns('',      
  url(r'^/user/(?P<username>\w+)/', include(profile_patterns)),
)