Django - 如何将多个参数传递给 url 模板标签
在我的 urls.py 中,我有:
(r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/section/(?P<slug>[-\w]+)/$',
'paper.views.issue_section_detail',
{},
'paper_issue_section_detail'
),
并且我正在尝试在模板中执行此操作:
{% url paper_issue_section_detail issue.pub_date.year,issue.pub_date.month,issue.pub_date.day,section_li.slug %}
但我收到此错误:
TemplateSyntaxError
Caught an exception while rendering: Reverse for 'paper_issue_section_detail' with arguments '(2010, 1, 22, u'business')' and keyword arguments '{}' not found.
但是,如果我将 URL 模式更改为仅需要一个参数,则它可以正常工作。 ie:
(r'^(?P<year>\d{4})/$',
'paper.views.issue_section_detail',
{},
'paper_issue_section_detail'
),
and:
{% url paper_issue_section_detail issue.pub_date.year %}
因此,当我使用“url”模板标记传递多个参数时,它似乎会抱怨 - 我在使用两个参数时遇到相同的错误。有没有不同的方法来传递多个参数?我尝试传递命名关键字参数,这会生成类似的错误。
就其价值而言,相关视图的开头如下:
def issue_section_detail(request, year, month, day, slug):
How do I pass more single argument to the url template tag?
In my urls.py I have:
(r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/section/(?P<slug>[-\w]+)/
and I'm trying to do this in a template:
{% url paper_issue_section_detail issue.pub_date.year,issue.pub_date.month,issue.pub_date.day,section_li.slug %}
but I get this error:
TemplateSyntaxError
Caught an exception while rendering: Reverse for 'paper_issue_section_detail' with arguments '(2010, 1, 22, u'business')' and keyword arguments '{}' not found.
However, if I change the URL pattern to only require a single argument it works fine. ie:
(r'^(?P<year>\d{4})/
and:
{% url paper_issue_section_detail issue.pub_date.year %}
So it seems to complain when I pass more than a single argument using the 'url' template tag - I get the same error with two arguments. Is there a different way to pass several arguments? I've tried passing in named keyword arguments and that generates a similar error.
For what it's worth, the related view starts like this:
def issue_section_detail(request, year, month, day, slug):
How do I pass more than a single argument to the url template tag?
,
'paper.views.issue_section_detail',
{},
'paper_issue_section_detail'
),
and I'm trying to do this in a template:
but I get this error:
However, if I change the URL pattern to only require a single argument it works fine. ie:
and:
So it seems to complain when I pass more than a single argument using the 'url' template tag - I get the same error with two arguments. Is there a different way to pass several arguments? I've tried passing in named keyword arguments and that generates a similar error.
For what it's worth, the related view starts like this:
How do I pass more than a single argument to the url template tag?
,
'paper.views.issue_section_detail',
{},
'paper_issue_section_detail'
),
and:
So it seems to complain when I pass more than a single argument using the 'url' template tag - I get the same error with two arguments. Is there a different way to pass several arguments? I've tried passing in named keyword arguments and that generates a similar error.
For what it's worth, the related view starts like this:
How do I pass more than a single argument to the url template tag?
, 'paper.views.issue_section_detail', {}, 'paper_issue_section_detail' ),and I'm trying to do this in a template:
but I get this error:
However, if I change the URL pattern to only require a single argument it works fine. ie:
and:
So it seems to complain when I pass more than a single argument using the 'url' template tag - I get the same error with two arguments. Is there a different way to pass several arguments? I've tried passing in named keyword arguments and that generates a similar error.
For what it's worth, the related view starts like this:
How do I pass more than a single argument to the url template tag?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我遇到了同样的问题(我正在使用 Django 1.3.1)并尝试了 Gregor Müllegger 的建议,但这不起作用,原因有两个:
因此唯一可行的解决方案是:
I had the same issue (I'm using Django 1.3.1) and tried Gregor Müllegger's suggestion, but that didn't work for two reasons:
Thus the only working solution was:
问题出在 url 配置的
/(?P\d{2})/
部分。它仅允许使用两位数字 (\d{2}
),而issue.pub_date.month
只能使用一位数字。您可以在 URL 中允许一位数字(但这将违反唯一 URL 的原则,
/2010/1/...
将与/2010/01/ 相同...
)或将两位数字传递给网址模板标签中的月份参数。您可以使用
date
过滤器来实现日期对象的一致格式。像这样使用 url 标签:查看月份和日期参数:它将始终显示为两位数字(如有必要,带有前导零)。查看 now 标签的文档 以了解
date
过滤器可以使用哪些选项。The problem lives in the
/(?P<month>\d{2})/
part of your url configuration. It only allows exactly two digits (\d{2}
) whileissue.pub_date.month
is only one digit.You can do either allow also one digit in the URL (but this will violate the principle of unique URLs,
/2010/1/...
would be the same as/2010/01/...
) or pass two digits to the month argument in your url templatetag.You can use the
date
filter to achieve a consistent formating of date objects. Use the url tag like this:Look at the month and day argument: It will be always displayed as two digits (with a leading zero if necessary). Have a look at the documentation of the now tag to see which options are possible for the
date
filter.您的月份表达式为
(?P\d{2})
,但您向其发送参数1
。1
与\d{2}
不匹配,因此网址解析程序找不到您的视图。尝试将月份表达式更改为
\d{1,2}
(或类似效果)。Your month expression is
(?P<month>\d{2})
, but you're sending it the argument1
. The1
doesn't match\d{2}
, so the url resolver isn't finding your view.Try changing the month expression to
\d{1,2}
(or something to that effect).