python Django URL.py 和索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1316682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 21:55:58  来源:igfitidea点击:

Django URL.py and the index

pythondjangodjango-urls

提问by Asinox

I want to know what is the best way to write in the URL.py. I am trying to get the index in this way: www.example.comwith (r'',index). But when I try r'', all pages in the website are going to the home page.

我想知道在URL.py. 我试图以这种方式获取索引:www.example.com使用(r'',index). 但是当我尝试时r'',网站中的所有页面都将转到主页。

Part of my url.py:

我的一部分url.py

(r'^index',homepages),
(r'',homepages),

Thanks :)

谢谢 :)

回答by Brian R. Bondy

Like this:

像这样:

 #...
 (r'^$', index),
 #...

回答by Ned Batchelder

Django URL matching is very powerful if not always as convenient as it could be. As Brian says, you need to use the pattern r'^$' to force your pattern to match the entire string. With r'', you are looking for an empty string anywhere in the URL, which is true for every URL.

Django URL 匹配功能非常强大,但它并不总是那么方便。正如布赖恩所说,您需要使用模式 r'^$' 来强制您的模式匹配整个字符串。使用 r'',您可以在 URL 中的任何位置查找空字符串,这对于每个 URL 都是正确的。

Django URL patterns nearly always start with ^ and end with $. You could in theory do some fancy URL matching where strings found anywhere in the URL determined what view function to call, but it's hard to imagine a scenario.

Django URL 模式几乎总是以 ^ 开头并以 $ 结尾。理论上,您可以进行一些奇特的 URL 匹配,其中 URL 中任何位置的字符串确定要调用的视图函数,但很难想象一个场景。