Reg Ex Django URL Conf

发布于 2024-09-06 10:32:50 字数 757 浏览 5 评论 0原文

这是我的 django URLconf:

urlpatterns = patterns('',
    ('^hello/$', hello),
    (r'^polls/$', 'mysite.polls.views.index'),
    (r'^polls/(?P<poll_id>\d+)/$', 'mysite.polls.views.detail'),
    (r'^polls/(?P<poll_id>\d+)/results/$', 'mysite.polls.views.results'),
    (r'^polls/(?P<poll_id>\d+)/vote/$', 'mysite.polls.views.vote'),
    (r'^admin/', include(admin.site.urls)),
)

我不明白这个正则表达式中的 r 的作用:

r'^polls/$

我不明白这个正则表达式的作用:

(?P<poll_id>\d+)

而且我不明白为什么:

(r'^admin/', include(admin.site.urls))

没有 $ 符号它仍然有效...

我不明白我必须添加什么 URLconf 才能查看 http://127.0.0.1:8000/ 下的站点

These is my django URLconf:

urlpatterns = patterns('',
    ('^hello/

I don't understand what the r in this regex does:

r'^polls/$

I don't understand what this Regex does:

(?P<poll_id>\d+)

And I don't understand why in:

(r'^admin/', include(admin.site.urls))

There is no $ sign and it still works...

I don't understand what URLconf I have to add, to see a site under http://127.0.0.1:8000/

, hello), (r'^polls/

I don't understand what the r in this regex does:


I don't understand what this Regex does:


And I don't understand why in:


There is no $ sign and it still works...

I don't understand what URLconf I have to add, to see a site under http://127.0.0.1:8000/

, 'mysite.polls.views.index'), (r'^polls/(?P<poll_id>\d+)/

I don't understand what the r in this regex does:


I don't understand what this Regex does:


And I don't understand why in:


There is no $ sign and it still works...

I don't understand what URLconf I have to add, to see a site under http://127.0.0.1:8000/

, 'mysite.polls.views.detail'), (r'^polls/(?P<poll_id>\d+)/results/

I don't understand what the r in this regex does:


I don't understand what this Regex does:


And I don't understand why in:


There is no $ sign and it still works...

I don't understand what URLconf I have to add, to see a site under http://127.0.0.1:8000/

, 'mysite.polls.views.results'), (r'^polls/(?P<poll_id>\d+)/vote/

I don't understand what the r in this regex does:


I don't understand what this Regex does:


And I don't understand why in:


There is no $ sign and it still works...

I don't understand what URLconf I have to add, to see a site under http://127.0.0.1:8000/

, 'mysite.polls.views.vote'), (r'^admin/', include(admin.site.urls)), )

I don't understand what the r in this regex does:

I don't understand what this Regex does:

And I don't understand why in:

There is no $ sign and it still works...

I don't understand what URLconf I have to add, to see a site under http://127.0.0.1:8000/

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

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

发布评论

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

评论(4

愁以何悠 2024-09-13 10:32:50

“r”表示“原始”字符串,这使得在尝试编写正则表达式时变得更容易(您最终不会转义转义字符)。 http://docs.python.org/library/re.html#raw -string-notation

就第二个问题而言,它创建一个由 1 个或多个数字组成的命名匹配组,并将该值作为“poll_id”传递给视图。 http://docs.djangoproject.com/en/1.2/ topic/http/urls/#named-groups

管理字符串上没有 $ 的原因是您希望将所有以 /admin 开头的 url 传递到管理应用程序。 $ 是定义字符串结尾的特殊字符。因此,如果有 $,则只有 url /admin 会传递到管理应用程序,而不是 /admin/foo 或 /admin/foo/bar。

The 'r' denotes a 'raw' string, which makes life easier when trying to write regexes (you don't end up escaping the escape characters). http://docs.python.org/library/re.html#raw-string-notation

As far as the second question goes, it creates a named match group of 1 or more digits, and passes that value to the view as 'poll_id'. http://docs.djangoproject.com/en/1.2/topics/http/urls/#named-groups

The reason there isn't a $ on the admin string is that you want all urls that start with /admin to be passed to the admin app. $ is a special character that defines the end of a string. So if there were an $, then only the url /admin would be passed to the admin app, not /admin/foo or /admin/foo/bar.

半岛未凉 2024-09-13 10:32:50

我的 python 正则表达式很生锈,但这里是:

r'^polls/$

^ 意思是开头。
$ 表示结尾

(?P\d+) 表示一个整数 \d+ ,在我的代码中它将被放入变量中poll_id

(r'^admin/', include(admin.site.urls))
没有 $ 因为您可能不希望 url 在那里结束。您希望将 admin/somethingelse 传递给您的 admin.sites.urls 类。

My python regex is rusty but here goes:

r'^polls/$

the ^ means starts with.
The $ means the end

(?P<poll_id>\d+) means an integer \d+ which in my code will be put into a variable poll_id

(r'^admin/', include(admin.site.urls))
doesn't have a $ because you may not want the url to end there. You want admin/somethingelse to be passed to your admin.sites.urls class.

雨后彩虹 2024-09-13 10:32:50

r 表示提供的字符串是原始字符串,应忽略转义字符。 (r'^admin/', include(admin.site.urls)) 行没有 $ 因为它是另一个 url confs 的包含。因此,结尾 $ 位于 admin.site.urls 中的某个位置。

The r means the provided string is raw and escape characters should be ignored. The (r'^admin/', include(admin.site.urls)) line has no $ because it is an include for another url confs. So the end $ is somewhere in the admin.site.urls.

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