Take the 2-minute tour ×

I am trying to create a simple application to return the remainder given two inputs from GET parameters. These inputs will be taken from the url query.

For example http://thisisanexample.appspot.com/?a=1&b=2 This should result in the answer 1 since the remainder of 2/1 = 1.

    a = self.request.get('a')
    b = self.request.get('b')
    c = 0
    if (int(a)>int(b)):

However, I am running into the following ValueError problem: ValueError: invalid literal for int() with base 10: ''

I am assuming the problem lies in the fact that the variables are previously unknown and may begin as or if not specified, stay as '' thus, creating the error. Any help would be much appreciated!

share|improve this question
What does self.response.out.write(type(a)) tell you? –  bos Mar 5 '12 at 10:13
Not sure about your maths. 2 mod 1 is 0, as 2/1=2 not 1. –  Daniel Roseman Mar 5 '12 at 10:25
hmmm for some reason it doesn't give me anything. it is responding if i give a super simple command like self.response.out.write(a) which gives me value of a... and you are right - edited it to the above but still same error –  Sean Lee Mar 5 '12 at 10:25
add comment

3 Answers

up vote 5 down vote accepted

GET parameters are stored in request.GET, not in the request itself, so you want to change your code to

a = request.GET.get('a')
b = request.GET.get('b')

And BTW, where did you write that code? What is self.request? If you're writing a view, it shoud be just:

    # access request here

request.get is not defined on a Django HttpRequest object...


GAE is not exactly Django. Citing GAE's documentation:

By default, get() returns the empty string ('')

So your code is correct, but instead of checking whether a or b are None, you should check for empty strings.

share|improve this answer
Thanks! I am using coderbuddy and GAE. Used the following to define self. from google.appengine.ext import webapp from google.appengine.ext.webapp.util import run_wsgi_app import os from google.appengine.ext.webapp import template class MainPage(webapp.RequestHandler): def get(self): –  Sean Lee Mar 5 '12 at 10:36
GAE is not django, you should change the tags and maybe specify GAE in the question title... See the updated answer –  StefanoP Mar 5 '12 at 10:42
Actually, using Class based views, self.request makes sense. See here: docs.djangoproject.com/en/dev/topics/class-based-views (But I'll leave it to you that it's not quite the matter here). –  Thomas Orozco Mar 5 '12 at 10:46
Agree, but get is still not defined on it :-) of curse this is true in Django, while in GAE it works differently, and Sean's code works well –  StefanoP Mar 5 '12 at 10:49
add comment

You have to use the GET dict for your query, as the get method you are using is a member of the QueryDict class. Also, in this case it might be useful to use the default parameter the get method provides:

    a = int(self.request.GET.get('a', 0))
    b = int(self.request.GET.get('b', 1))
except TypeError:
    # Could not cast to int
# Do what you want
share|improve this answer
Ah this is very helpful! Sorry to seem noobish but are the 0 and 1 predefined values? –  Sean Lee Mar 5 '12 at 10:42
Yes, 0 and 1 are the values a and b will default to if they are not found in the GET dict. –  nijansen Mar 5 '12 at 10:49
add comment

If you use get_range (see http://code.google.com/intl/en/appengine/docs/python/tools/webapp/requestclass.html#Request_get_range), you can omit the manual conversion to int:

share|improve this answer
add comment

Your Answer


