Django url pattern

2019-08-02 19:10发布

This should be an easy question. I have two url patterns in Django:

url(r'^wiki/page/(?P<page_title>.*)', views.wiki_view, name = 'wiki_view'),
url(r'^wiki/page/$', views.wiki_page_index, name = 'wiki_page_index'),

When I visit /wiki/page/test, it takes me to views.wiki_view. This is correct. I need the first pattern to capture all characters after the "page/", which is why I used .*

When I visit /wiki/page/, it also takes me to views.wiki_view. This is incorrect.

I could alter the second url pattern to read:

url(r'^wiki/page/$', views.wiki_page_index, name = 'wiki_page_index'),

Thus, when I visit /wiki/page, it will take me to views.wiki_page_index. But I'd rather fix the problem instead of avoiding it.

How do I format the first url pattern so that it won't pick up the instance of /wiki/page/?

标签: django url
2条回答
混吃等死
2楼-- · 2019-08-02 19:37

Change the first one to:

url(r'^wiki/page/(?P<page_title>.+)', views.wiki_view, name = 'wiki_view'),
查看更多
时光不老,我们不散
3楼-- · 2019-08-02 19:53

Place the second one before the current first one.

http://docs.djangoproject.com/en/dev/topics/http/urls/ ("Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL.")

查看更多
登录 后发表回答