Django - 如何将多个参数传递给 url 模板标签

发布于 2024-08-19 08:48:04 字数 1195 浏览 6 评论 0原文

在我的 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 技术交流群。

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

发布评论

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

评论(3

鯉魚旗 2024-08-26 08:48:04

我遇到了同样的问题(我正在使用 Django 1.3.1)并尝试了 Gregor Müllegger 的建议,但这不起作用,原因有两个:

  • 在年、月和日值之间不应该有逗号
  • 我的基于类的通用视图似乎 仅采用关键字参数

因此唯一可行的解​​决方案是:

{% url news_detail slug=object.slug year=object.date|date:"Y" month=object.date|date:"m" day=object.date|date:"d" %}

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:

  • there should be no commas between year, month and day values
  • my class-based generic view seems to take only keyword arguments

Thus the only working solution was:

{% url news_detail slug=object.slug year=object.date|date:"Y" month=object.date|date:"m" day=object.date|date:"d" %}
病毒体 2024-08-26 08:48:04

问题出在 url 配置的 /(?P\d{2})/ 部分。它仅允许使用两位数字 (\d{2}),而 issue.pub_date.month 只能使用一位数字。

您可以在 URL 中允许一位数字(但这将违反唯一 URL 的原则,/2010/1/... 将与 /2010/01/ 相同...)或将两位数字传递给网址模板标签中的月份参数。
您可以使用date 过滤器来实现日期对象的一致格式。像这样使用 url 标签:

{% url paper_issue_section_detail issue.pub_date|date:"Y",issue.pub_date|date:"m",issue.pub_date|date:"d",section_li.slug %}

查看月份和日期参数:它将始终显示为两位数字(如有必要,带有前导零)。查看 now 标签的文档 以了解date 过滤器可以使用哪些选项。

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.

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.

柠栀 2024-08-26 08:48:04

您的月份表达式为 (?P\d{2}),但您向其发送参数 11\d{2} 不匹配,因此网址解析程序找不到您的视图。

尝试将月份表达式更改为 \d{1,2} (或类似效果)。

Your month expression is (?P<month>\d{2}), but you're sending it the argument 1. The 1 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).

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