Django url 问题

比如下面的设置是错误的,http://localhost:8000/user/userlist/ 永远访问不了访问 userlist页面,因为它其实包含于第二条,所以程序总是会执行第二条,而且第二条url需要参数,这里又没有名叫userlist的user

urlpatterns = patterns('',
  
    url(r'^$', views.hello, name='hello'),
    url(r'^(?P<username>\w+)$', views.articlepage, name='articlepage'),
    url(r'^userlist/$', views.userlist, name='userlist'),
    
    

)

解决方法 一  交换位置

访问路径不用变化,但万一有用户起名叫userlist就bug了

urlpatterns = patterns('',
  
    url(r'^$', views.hello, name='hello'),
    url(r'^userlist/$', views.userlist, name='userlist'),
    url(r'^(?P<username>\w+)$', views.articlepage, name='articlepage'),
    
    
    

)

 解决办法二  让有冲突的url分层

访问user的路径多了一层userlist

urlpatterns = patterns('',
  
    url(r'^$', views.hello, name='hello'),
    url(r'^userlist/(?P<username>\w+)$', views.articlepage, name='articlepage'),
    url(r'^userlist/$', views.userlist, name='userlist'),
   

)

终于找到原因了

相关推荐