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'), )
终于找到原因了
相关推荐
aweilark 2020-06-14
86193952 2020-10-27
小木兮子 2020-11-11
HeyShHeyou 2020-11-17
seanzed 2020-10-15
huangliuyu00 2020-09-24
libaoshan 2020-09-11
zhangpan 2020-09-10
chongxiaocheng 2020-08-16
xcguoyu 2020-08-15
Qizonghui 2020-08-02
ldcwang 2020-07-26
mqfcu 2020-07-21
jeason 2020-07-20
sunzhihaofuture 2020-07-19
knightwatch 2020-07-19
点滴技术生活 2020-07-19
Reiki 2020-07-06