问题描述
在我的urls.py中,我有:
In my urls.py I have:
(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'
),
我正试图在模板中执行此操作:
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 %}
但是我收到这个错误:
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模式更改为仅需要一个参数就可以正常工作。即:
However, if I change the URL pattern to only require a single argument it works fine. ie:
(r'^(?P<year>\d{4})/$',
'paper.views.issue_section_detail',
{},
'paper_issue_section_detail'
),
和:
{% url paper_issue_section_detail issue.pub_date.year %}
所以当我使用'url'模板标签传递多个参数时,似乎抱怨 - 我得到相同的错误有两个参数。有不同的方法来传递几个论据?我尝试传递命名关键字参数,并产生类似的错误。
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):
如何将更多的参数传递给url模板标签?
How do I pass more than a single argument to the url template tag?
推荐答案
问题存在于您的url配置的 /(?P< month> \d {2})/
中。它只允许两位数字( \d {2}
),而 issue.pub_date.month
只有一位数字
The problem lives in the /(?P<month>\d{2})/
part of your url configuration. It only allows exactly two digits (\d{2}
) while issue.pub_date.month
is only one digit.
您可以同时允许网址中的一位数字(但这将违反唯一网址的原则, / 2010/1 /。 ..
将与 / 2010/01 /...
相同),或将两位数字传递到您的url templatetag中的月份参数。 br>
您可以使用日期
过滤器来实现日期对象的一致化。使用这样的URL标签:
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:
{% url paper_issue_section_detail issue.pub_date|date:"Y",issue.pub_date|date:"m",issue.pub_date|date:"d",section_li.slug %}
在月和日参数:它将始终显示为两位数(如果需要,前导零)。查看以查看日期
过滤器可以使用哪些选项。
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.
这篇关于Django - 如何传递几个参数到url模板标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!