如何将多个参数传递给命名组url

时间:2015-08-20 17:22:12

标签: python django

我不知道如何通过模板URL反向查找传递多个参数

<a href="{% url 'planner_by_location' {'pk':location.pk,'year':date_single[:3],'month':date_single[-6:5], 'day':date_single[-11:9]}  %}">test</a>

匹配此网址格式:

r'^something/date/(?P<pk>\d+)/(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/$'

获取错误:

Could not parse the remainder: '{"pk":location.pk,' from '{"pk":location.pk,'

有人有线索吗?

1 个答案:

答案 0 :(得分:0)

Django模板语言不是python。您不能将字典作为参数传递,也不能使用方括号来切换变量。

文档中的示例显示了如何将命名参数传递给url标记。

{% url 'some-url-name' arg1=v1 arg2=v2 %}

您也可以使用切片过滤器。

year=date_single|slice:":3"

https://docs.djangoproject.com/en/1.8/ref/templates/builtins/#url https://docs.djangoproject.com/en/1.8/ref/templates/builtins/#slice

相关问题