Django URL.py和索引

时间:2021-07-05 18:07:59

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.com with (r'',index). But when I try r'', all pages in the website are going to the home page.

我想知道在URL.py中写入的最佳方式是什么。我试图以这种方式得到索引:www.example.com with(r'',index)。但是当我尝试r''时,网站上的所有页面都会进入主页。

Part of my url.py:

我的url.py的一部分:

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

Thanks :)

谢谢 :)

2 个解决方案

#1


32  

Like this:

喜欢这个:

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

#2


5  

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匹配非常强大,如果不总是那么方便。正如Brian所说,你需要使用模式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中的任何地方找到的字符串确定了要调用的视图函数,但是很难想象一个场景。

#1


32  

Like this:

喜欢这个:

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

#2


5  

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匹配非常强大,如果不总是那么方便。正如Brian所说,你需要使用模式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中的任何地方找到的字符串确定了要调用的视图函数,但是很难想象一个场景。