Django urlpatterns 令人沮丧的尾部斜杠问题

发布于 2024-10-01 20:35:06 字数 1078 浏览 2 评论 0原文

我能找到的 django 站点的 urlpatterns 的所有示例都有一个单独的条目,用于没有前导斜杠或根文件夹的传入 url。然后他们处理每行上的子文件夹。我不明白为什么一个简单的

/?

正则表达式不允许它们出现在一个简单的行上。

考虑以下情况,我们将 Django 项目称为 Baloney,应用程序名称为 Cheese。因此,在项目 urls.py 中,我们有类似的东西来允许应用程序 urls.py 处理它的请求...

urlpatterns = patterns('',
    (r'^cheese/', include('Baloney.Cheese.urls')),
)

然后在奶酪应用程序 urls.py 内部,我不明白为什么这一简单的行不会触发,因为对于所有传入 url 子路径(包括空白值)为 true...

urlpatterns = patterns('',
    (r'^(?P<reqPath>.*)/?$', views.cheeseapp_views),
)

相反,它匹配空白大小写,但不匹配存在值的大小写。所以...

http://baloneysite.com/cheese/        -->   MATCHES THE PATTERN
http://baloneysite.com/cheese/swiss   -->   DOES NOT MATCH

基本上我想捕获 reqPath 变量以包含其中的任何内容(甚至是空白或“”),但不包含任何尾随斜杠(如果有)。

url 是从数据库中提取的动态 slugs,因此我对视图中的内容进行了所有匹配,只需要 url 模式来转发值。我知道以下内容有效,但不明白为什么不能将其全部与 / 放在一行上?结尾 $ 符号之前的正则表达式。

(r'^$', views.cheeseapp_views, {'reqPath':''}),
(r'^(?P<reqPath>.*)/$', views.cheeseapp_views),

感谢任何见解。

All of the examples I can find of urlpatterns for django sites have a separate entry for incoming urls that have no leading slash, or the root folder. Then they handle subfolders on each individual line. I don't understand why a simple

/?

regular expression doesn't permit these to be on one simple line.

Consider the following, let's call the Django project Baloney and the App name is Cheese. So in the project urls.py we have something like this to allow the apps urls.py to handle it's requests...

urlpatterns = patterns('',
    (r'^cheese/', include('Baloney.Cheese.urls')),
)

then inside of the Cheese apps urls.py, I don't understand why this one simple line would not trigger as true for all incoming url subpaths, including a blank value...

urlpatterns = patterns('',
    (r'^(?P<reqPath>.*)/?

Instead, it matches the blank case, but not the case of a value present. So...

http://baloneysite.com/cheese/        -->   MATCHES THE PATTERN
http://baloneysite.com/cheese/swiss   -->   DOES NOT MATCH

Basically I want to capture the reqPath variable to include whatever is there (even blank or '') but not including any trailing slash if there is one.

The urls are dynamic slugs pulled from the DB so I do all the matching up to content in my views and just need the url patterns to forward the values along. I know that the following works, but don't understand why this can't all be placed on one line with the /? regular expression before the ending $ sign.

(r'^

Appreciate any insights.

, views.cheeseapp_views), )

Instead, it matches the blank case, but not the case of a value present. So...


Basically I want to capture the reqPath variable to include whatever is there (even blank or '') but not including any trailing slash if there is one.

The urls are dynamic slugs pulled from the DB so I do all the matching up to content in my views and just need the url patterns to forward the values along. I know that the following works, but don't understand why this can't all be placed on one line with the /? regular expression before the ending $ sign.


Appreciate any insights.

, views.cheeseapp_views, {'reqPath':''}), (r'^(?P<reqPath>.*)/

Appreciate any insights.

, views.cheeseapp_views), )

Instead, it matches the blank case, but not the case of a value present. So...

Basically I want to capture the reqPath variable to include whatever is there (even blank or '') but not including any trailing slash if there is one.

The urls are dynamic slugs pulled from the DB so I do all the matching up to content in my views and just need the url patterns to forward the values along. I know that the following works, but don't understand why this can't all be placed on one line with the /? regular expression before the ending $ sign.

Appreciate any insights.

, views.cheeseapp_views),

Appreciate any insights.

, views.cheeseapp_views), )

Instead, it matches the blank case, but not the case of a value present. So...

Basically I want to capture the reqPath variable to include whatever is there (even blank or '') but not including any trailing slash if there is one.

The urls are dynamic slugs pulled from the DB so I do all the matching up to content in my views and just need the url patterns to forward the values along. I know that the following works, but don't understand why this can't all be placed on one line with the /? regular expression before the ending $ sign.

Appreciate any insights.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不气馁 2024-10-08 20:35:06

我刚刚尝试了一个类似的示例,它按照您编写的方式工作。不需要 /?, .* 无论如何都会匹配。您收到的确切错误是什么?也许您有没有请求参数的视图?即views.cheeseapp_views应该类似于:

def cheeseapp_views(request, reqPath):
    ...

编辑:

您建议的模式将尾部斜杠捕获到reqPath中,因为*运算符是贪婪的(看看docs.python.org/library/re.html)。试试这个:

(r'^(?P<reqPath>.*?)/?

注意它是.*?而不是 .* 以使其非贪婪。

, views.cheeseapp_views)

注意它是.*?而不是 .* 以使其非贪婪。

I just tried a similar sample and it worked as you wrote it. No need for /?, .* would match that anyway. What is the exact error you are getting? Maybe you have your view without the request parameter? I.e. views.cheeseapp_views should be something like:

def cheeseapp_views(request, reqPath):
    ...

Edit:

The pattern that you suggested catches the trailing slash into reqPath because * operator is greedy (take a look at docs.python.org/library/re.html). Try this instead:

(r'^(?P<reqPath>.*?)/?

note it's .*? instead of .* to make it non-greedy.

, views.cheeseapp_views)

note it's .*? instead of .* to make it non-greedy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文