help with "Key 'timeout' not found in <QueryDict: {}>"

164 views
Skip to first unread message

psychok7

unread,
May 3, 2012, 6:22:51 PM5/3/12
to django...@googlegroups.com
hi i have done a succefull query and i converted the results into links so i can make a post by clicking on the links to make a reservation.

my problem is, theres a variable timeout that i want to specify manually, but i get a "Key 'timeout' not found in <QueryDict: {}>"

i know i am supposed to pass that variable but i just dont know how.

my code is :

def reserve (request,user_id,park_id):
    #only works if the user exists in database
    u = get_object_or_404(User, pk=user_id)
    n = get_object_or_404(Notification, pk=park_id)
    p = get_object_or_404(Park, pk=1)
    r = Reservation(user=u ,notification=n, park=p, timeout=int(request.POST['timeout']),active=True,parked=False,reservation_date=datetime.now())
    r.save()
    return HttpResponse('Spot reserved')

html:

{% if parks %} <p>You searched for: <strong>{{ query }}</strong></p> <p>Found {{ parks|length }} park{{ parks|pluralize }}.</p> <ul> {% for park in parks %} <input type="text" name="timeout" value="60" id="timeout"> <li><a href="/accounts/{{ user.id }}/reserve/{{ park.id }}">{{ park.park_address }}</a></li> {% endfor %} </ul> {% endif %}

can someone help me?

Kurtis Mullins

unread,
May 3, 2012, 6:58:19 PM5/3/12
to django...@googlegroups.com
Can we see what your "Reservation" model looks like? Specifically, that 'timeout' field?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/iTBJptqGhyoJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

psychok7

unread,
May 3, 2012, 7:47:36 PM5/3/12
to django...@googlegroups.com
timeout field is just an INTEGER.. just saying the amount of time the space will be reserved.

class Reservation(models.Model):
    
    user = models.ForeignKey(User)
    park = models.ForeignKey(Park)
    notification = models.ForeignKey(Notification)

    timeout = models.IntegerField()
    active = models.BooleanField()
    parked = models.BooleanField()
    reservation_date = models.DateTimeField('date reserved')

On Thursday, May 3, 2012 11:58:19 PM UTC+1, Kurtis wrote:
Can we see what your "Reservation" model looks like? Specifically, that 'timeout' field?

hi i have done a succefull query and i converted the results into links so i can make a post by clicking on the links to make a reservation.

my problem is, theres a variable timeout that i want to specify manually, but i get a "Key 'timeout' not found in <QueryDict: {}>"

i know i am supposed to pass that variable but i just dont know how.

my code is :

def reserve (request,user_id,park_id):
    #only works if the user exists in database
    u = get_object_or_404(User, pk=user_id)
    n = get_object_or_404(Notification, pk=park_id)
    p = get_object_or_404(Park, pk=1)
    r = Reservation(user=u ,notification=n, park=p, timeout=int(request.POST['timeout']),active=True,parked=False,reservation_date=datetime.now())
    r.save()
    return HttpResponse('Spot reserved')

html:

{% if parks %} <p>You searched for: <strong>{{ query }}</strong></p> <p>Found {{ parks|length }} park{{ parks|pluralize }}.</p> <ul> {% for park in parks %} <input type="text" name="timeout" value="60" id="timeout"> <li><a href="/accounts/{{ user.id }}/reserve/{{ park.id }}">{{ park.park_address }}</a></li> {% endfor %} </ul> {% endif %}

can someone help me?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/iTBJptqGhyoJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.

Kurtis Mullins

unread,
May 4, 2012, 4:00:43 AM5/4/12
to django...@googlegroups.com, psychok7
ahh, okay. Let’s try another approach to see if you can nail down that error. I’m not sure why it’s not working.
 
def reserve(request, park_id):
   
    # Get the User
    # This is different than the method you’re using
    # but I think it may be what you want.
 
    user = self.request.user
 
    # Grab that Notification and Park
    # In your method, you were throwing a 404 error. I’m
    # not sure if that’s what you’re actually after (page not found)
    # or if you’d rather give a server error because the user attempted
    # to use a notification or park that didn’t exist.
 
    # Also, I’m a bit confused by the variable names, haha. I’d think the Park
    # object would take the park_id and the Notification object would use
    # something else.
 
    notification = Notification.objects.get(pk = park_id)
    park = Park.objects.get(pk = 1)
 
    # This will check to see if that POST Variable is available.
    if ’timeout’ is in request.POST:
 
        # Grab the Timeout Variable
        timeout = int(request.POST[’timeout’])
 
        # Let’s build the Reservation object piece by piece to see what the problem is.
        reservation = Reservation.objects.create()
        reservation.user = user
        # ....
        reservation.notification = notification
        reservation.timeout = timeout
        # ...
        reservation.save()
 
        return HttpResponse(...)
 
    # This is the error case where the timeout wasn’t POST'ed.
    else:
        raise Exception(”The timeout variable wasn’t posted.”)
 
This time, you should see exactly where that error is popping up. Also, ModelForm + CreateView would be a great an easy combination for this exact situation.
       
Good luck!
-Kurtis Mullins
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/KGbfHKRKmrYJ.

To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.

Jani Tiainen

unread,
May 4, 2012, 3:58:58 AM5/4/12
to django...@googlegroups.com

4.5.2012 1:22, psychok7 kirjoitti:
> hi i have done a succefull query and i converted the results into links
> so i can make a post by clicking on the links to make a reservation.

No, you haven't. When you click you'll still send very much standard GET
request to server. You can and should verify that by using some
debugging console.

> my problem is, theres a variable timeout that i want to specify
> manually, but i get a "Key 'timeout' not found in <QueryDict: {}>"
>
> i know i am supposed to pass that variable but i just dont know how.

> my code is :
>
> def reserve (request,user_id,park_id):
> #only works if the user exists in database
> u = get_object_or_404(User, pk=user_id)
> n = get_object_or_404(Notification, pk=park_id)
> p = get_object_or_404(Park, pk=1)
> r = Reservation(user=u ,notification=n, park=p,
> timeout=int(request.POST['timeout']),active=True,parked=False,reservation_date=datetime.now())
> r.save()
> return HttpResponse('Spot reserved')
>
> html:
>
> {% if parks %} <p>You searched for: <strong>{{ query }}</strong></p>
> <p>Found {{ parks|length }} park{{ parks|pluralize }}.</p> <ul> {% for
> park in parks %} <input type="text" name="timeout" value="60"
> id="timeout"> <li><a href="/accounts/{{ user.id }}/reserve/{{ park.id
> }}">{{ park.park_address }}</a></li> {% endfor %} </ul> {% endif %}
>
> can someone help me?

If above is your true HTML and nothing removed you're just creating list
of standard links.

If you want to pass additional parameters there is few ways to achieve it:

a) You can modify your sent url by javascript and add timeout manually
to get query parameters.

b) You transform your links to selectable list/dropdown and create real
HTML form to send post with real submit button.

c) Hybrid of a and b. I won't go into details since it would be just a mess.

--
Jani Tiainen

- Well planned is half done and a half done has been sufficient before...

Kurtis Mullins

unread,
May 4, 2012, 1:49:14 PM5/4/12
to django...@googlegroups.com
Jani is right, I didn't take a close look at that HTML. Using this (without any Javascript), you're not submitting a form with method="post" so you won't actually have any of that request.POST data available that you're looking for.

--
You received this message because you are subscribed to the Google Groups "Django users" group.

psychok7

unread,
May 4, 2012, 4:56:39 PM5/4/12
to django...@googlegroups.com
thanks guys it worked. i did a POST from and it worked ;)

cheers

psychok7

unread,
May 4, 2012, 5:02:58 PM5/4/12
to django...@googlegroups.com, psychok7
by the way kurtis,  i had a bug in the park_id and didn't even notice that cause it was working lol, u solve it for me :P
Sent: Thursday, May 03, 2012 4:47:56 PM
Reply all
Reply to author
Forward
0 new messages