Django URL模式 [英] Django url pattern

查看:33
本文介绍了Django URL模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该是一个简单的问题.我在Django中有两个网址格式:

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'),

当我访问/wiki/page/test时,它带我进入views.wiki_view.这是对的.我需要第一个模式来捕获"page/"之后的所有字符,这就是我使用.*

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 .*

当我访问/wiki/page/时,它也将我带到views.wiki_view.这是不正确的.

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'),

因此,当我访问/wiki/page时,它将带我到views.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.

如何设置第一个网址格式的格式,以使它不会占用/wiki/page/的实例?

推荐答案

将第二个放在当前第一个之前.

Place the second one before the current first one.

http://docs.djangoproject.com/en/dev/topics/http/urls/("Django按顺序遍历每个URL模式,并在与所请求URL匹配的第一个URL模式处停止."

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.")

这篇关于Django URL模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆