我在URLconf中有以下网址:
url(r'^events(/((?P<day>\d{2})(?P<month>\d{2})(?P<year>\d{4}))?/(?P<company_uuid>[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})?)?$',
'events',
name='events'),
和views.py:
@login_required
def events(request, day=None, month=None, year=None, company_uuid=None):
today = now()
if company_uuid:
return HttpResponse(company_uuid)
日,月,年和company_uuid是可选参数。 所以,我可以导航到
但是在模板的某个地方,我想使用反向网址并建立链接,如:
<a class="btn" href="{% url 'events' company_uuid=company.uuid %}">Show all company events</a>
显示没有指定日期的所有公司活动。
我得到的只是错误:
NoReverseMatch at /companies
Reverse for 'events' with arguments '()' and keyword arguments '{u'company_uuid': u'28b68080-d336-11e2-b53f-001e681fda9d'}' not found.
我该如何解决?
答案 0 :(得分:2)
试试这个:
url(
r'^events/(?:(?P<year>\d{4})/)?(?:(?P<month>\d{2})/)?(?:(?P<day>\d{2})/)?(?:(?P<company_uuid>[a-f0-9\-]{36})/)?$',
'events',
name='events'),
我做了以下更改。
company_uuid
模式,如果需要,可以切换回您的模式events/2013/12/01/
而不是events/20131201/
year -> month -> day
而非day -> month -> year
以下模式将匹配:
events/{year}/
events/{year}/{month}/
events/{year}/{month}/{day}/
events/{year}/{month}/{day}/{company_uuid}/
现在可以在模板中执行
{% url 'events' year=2013 %}
{% url 'events' year=2013 month=6 %}
{% url 'events' year=2013 month=6 day=13 %}
{% url 'events' year=2013 month=6 day=13 company_uuid='28b68080-d336-11e2-b53f-001e681fda9d' %}
{% url 'events' company_uuid='28b68080-d336-11e2-b53f-001e681fda9d' %}