开发者

Django - How to pass several arguments to the url template tag

开发者 https://www.devze.com 2022-12-17 16:23 出处:网络
In my urls.py I have: (r\'^(?P<year>\\d{4})/(?P<month>\\d{2})/(?P<day>\\d{2})/section/(?P<slug>[-\\w]+)/$\',

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 %}

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})/$', 
    'paper.views.issue_section_detail', 
    {}, 
    'paper_issue_section_detail'
),

and:

{% url paper_issue_section_detail issue.pub_date.ye开发者_如何学Goar %}

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?


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" %}


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.


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消