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

后端 未结 3 1511
梦如初夏
梦如初夏 2021-02-12 11:15

In my urls.py I have:

(r\'^(?P\\d{4})/(?P\\d{2})/(?P\\d{2})/section/(?P[-\\w]+)/$\', 
    \'paper.views.issue_sec         


        
3条回答
  •  有刺的猬
    2021-02-12 11:50

    The problem lives in the /(?P\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.

提交回复
热议问题