views:

2172

answers:

3

I received the following error when trying to retrieve data using Google App Engine from a single entry to a single page e.g. foobar.com/page/1 would show all the data from id 1:

ValueError: invalid literal for int() with base 10

Here are the files:

Views.py

class One(webapp.RequestHandler):    
    def get(self, id):
        id         = models.Page.get_by_id(int(str(self.request.get("id")))) 
        page_query = models.Page.get(db.Key.from_path('Page', id))
        pages      = page_query

        template_values = {
            'pages': pages,
        }

        path = os.path.join(os.path.dirname(__file__), 'template/list.html')
        self.response.out.write(template.render(path, template_values))

Urls.py:

(r'/browse/(\d+)/', One),

Error:

Traceback (most recent call last):
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/webapp/__init__.py", line 501, in __call__
    handler.get(*groups)
  File "/Volumes/foobar/views.py", line 72, in get
    id = models.Page.get_by_id(int(str(self.request.get("id"))))
ValueError: invalid literal for int() with base 10: ''
A: 

Change self.request.get("id") to simply id, which is already being passed to your get handler.

The code, as you have it, would only work for URLs like /browse/1/?id=1

Triptych
Thanks for your help, now on the error.BadArgumentError: Expected an integer id or string name as argument 2; received <models.Page object at 0x7d65f70> (a Page).
gpjones
+1  A: 

I'm not quite sure what you're trying to achieve here. The first line:

    id         = models.Page.get_by_id(int(str(self.request.get("id"))))

returns a Page object with an ID fetched from the query string. To make it work with the passed in argument, change it to:

    id         = models.Page.get_by_id(int(id))

Odder is the second line:

    page_query = models.Page.get(db.Key.from_path('Page', id))

This does not return a query - it returns a page object, and if you replace 'id' with 'int(id)' does precisely the same thing as the first line. What are you trying to achieve here?

Nick Johnson
I'm trying to pull back a single entry onto the page.E.g. localhost:8080/browse/1/ - would display entry 1 localhost:8080/browse/2/ - would display entry 2 etc etc..list.html contains:{% for page in pages %} {{ page.title }}{% endfor %}
gpjones
A: 

I had a similar error in one of my code. I just did a simple hack of converting it into decimal first and later converting it into int int(Decimal(str(self.request.get("id"))))

Stattrav