Ahmed
Ahmed

Reputation: 97

Passing time as argument in django url

in my view I have an argument of time to be passed but how do i pass it through a django url

my view is

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

and i tried a url

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

I called it as

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

but it doesnt work and gives error Could not parse the remainder: ':30:00' from '08:30:00'

Upvotes: 1

Views: 231

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476594

The problem is that you pass 08:30:30 as a "raw expression". But Django templates fail to understand what you do with that colon.

You need to pass the parameter as a string, like:

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

Since your URL contains a time parameter, you need to handle it in the view:

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

Note that this view makes changes to the entities, and therefore this normally should be handled by a POST request. GET requests are normally not supposed to make (significant) changes.

Upvotes: 1

Related Questions