在Django URL中将时间作为参数传递

时间:2018-10-05 13:58:57

标签: django

在我看来,我有一个要传递时间的参数,但是如何通过Django URL传递它

我的观点是

def create_event(check):
TimeSlots.objects.create(start=check)
return HttpResponseRedirect("index.html")

我尝试了一个网址

url(r'^new/(?P<time>\d{2}:\d{2}:\d{2})/$', views.create_event, name='check'),

我称它为

<a href="{% url 'check' 08:30:00 %}">click</a>

但是它不起作用并且给出错误 无法解析“ 08:30:00”中的其余部分:“:30:00”

1 个答案:

答案 0 :(得分:1)

问题是您将08:30:30作为“原始表达式”传递。但是Django模板无法理解您对该冒号的处理方式。

您需要将参数作为字符串传递,例如:

<a href="{% url 'check' time='08:30:00' %}">click</a>
<!--                         ^ quote  ^  -->

由于您的网址包含一个time参数,因此您需要在视图中对其进行处理:

def create_event(request, time):
    TimeSlots.objects.create(start=time)
    return HttpResponseRedirect("index.html")

请注意,此视图对实体进行了更改,因此通常应由POST请求处理。通常不应该对GET请求进行(重大)更改。