如何在 Django 中将 object_id 传递给通用视图 object_detail
我正在使用 django.views.generic.list_detail.object_detail。
根据文档,视图采用变量 object_id。为此,我将以下内容添加到我的 urlconf 中:
(r'^(?P<object_id>\d+)$', list_detail.object_detail, article_info),
上面的行位于主 urlconf 中包含的单独 urlconf 中。
如果我将“^”字符留在模式的开头,然后尝试转到地址:
.../?object_id=1
它不起作用。如果我删除“^”字符,地址:
.../?object_id=1
仍然不起作用。但是,如果我使用:
.../object_id=1 (不带问号)
该视图接受 object_id 变量并且可以正常工作。我对此有两个问题。
第一:是否可以使用包含的 urlconf 中的“^”字符将模式限制为仅匹配基本 url 模式以及包含的 urlconf 中 ^$ 之间的确切字符串?
第二:为什么问号字符会阻止视图接收“object_id”变量?我以为是“?”用于指定 URL 中的 GET 变量。
谢谢
I'm using django.views.generic.list_detail.object_detail.
According to the documentation the view takes the variable object_id. To do this I added the following to my urlconf:
(r'^(?P<object_id>\d+)
The above line is in a separate urlconf that is included in the main urlconf.
If I leave the '^' character at the beginning of the pattern and then attempt to go to the address:
.../?object_id=1
It does not work. If I remove the '^' character the address:
.../?object_id=1
Still does not work. However if I use:
.../object_id=1 (without the question mark)
The view accepts the object_id variable and works without a problem. I have two questions about this.
First: Can the '^' character in an included urlconf be used to restrict the pattern to only match the base url pattern plus the exact string bettween a ^$ in the included urlconf?
Second: Why does the question mark character stop the view from receiving the 'object_id' variable? I thought the '?' was used to designate GET variables in a URL.
thanks
, list_detail.object_detail, article_info),
The above line is in a separate urlconf that is included in the main urlconf.
If I leave the '^' character at the beginning of the pattern and then attempt to go to the address:
.../?object_id=1
It does not work. If I remove the '^' character the address:
.../?object_id=1
Still does not work. However if I use:
.../object_id=1 (without the question mark)
The view accepts the object_id variable and works without a problem. I have two questions about this.
First: Can the '^' character in an included urlconf be used to restrict the pattern to only match the base url pattern plus the exact string bettween a ^$ in the included urlconf?
Second: Why does the question mark character stop the view from receiving the 'object_id' variable? I thought the '?' was used to designate GET variables in a URL.
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我先解决你的第二个问题。此上下文中的
?
字符用于表示正则表达式中的命名组。这是Python 提供的正则表达式的自定义扩展。 (请参阅操作方法 例如)要传递
object_id
将其附加到 URL(在您的情况下)。像这样:../foo/app/3
,其中3
是object_id
。I'll tackle your second question first. The
?
character in this context is used to denote a named group in the regular expression. This is a custom extension to regular expressions provided by Python. (See the howto for examples)To pass an
object_id
append it to the URL (in your case). Like this:../foo/app/3
where3
is theobject_id
.该 urlconf 告诉 Django 将
.../1
、.../123
类型的 URL 映射到给定视图 (...
是该 urlconf 的前缀)。(?P\d+)
告诉 django 将\d+
捕获的值分配给变量object_id
。另请参阅关于 正则表达式 的 Python 文档和关于其 URL 调度程序。That urlconf tells Django to map URLs of the kind
.../1
,.../123
to the given view (...
being the prefix of that urlconf).(?P<object_id>\d+)
tells django to assign the value captured by\d+
to the variableobject_id
. See also Python's documentation on regular expressions and django's documentation on its URL dispatcher.首先,字符串前面的“r”表示它是一个正则表达式,^表示字符串的开头,$表示字符串的结尾。在 python 中,当你输入
(?P<'something>a_regular_expression)
python 会在该字符串中找到 a_regular_expression 的匹配表达式,并将其作为名称为 some 的变量返回。这里 \d+ 表示数字,它会找到一个数字并将其传递给您通过 object_id 名称分配给那里的函数(article_info)。其次,您不必担心 GET url,您只需设置主 url,django 将自行管理 GET 变量。例如,如果您的网址模式中有
(r'^/post/$, my_app.views.show_post)
并且您发送此获取请求../post/?id=10< /code>,django将使用你的
my_app.views.show_post
函数,你可以访问request.GET中的get变量,这里如果你想获取id你可以使用这个request.GET [id]
。first, the "r" before a string means it is a regular expression and ^ means the start of the string, $ means the end of string. in python when you put
(?P<'something>a_regular_expression)
python will find your matched expression for a_regular_expression in that string and return it as a variable with name: something. here \d+ means numbers, and it will find a number and pass it to the function you assigned there ( article_info ) by the name of object_id.second, you shouldn't worry for GET urls, you just set the main url and django will manage GET variables itself. for example if you have
(r'^/post/$, my_app.views.show_post)
in your url patterns and you send this get request../post/?id=10
, django will use yourmy_app.views.show_post
function and you can access the get variables in request.GET, here if you want to get id you can use thisrequest.GET[id]
.