Milap
Milap

Reputation: 133

How to pass multiple arguments to handlers in Google App Engine python?

I was trying to build a wiki with the ability to see previous edits. Every wiki entry has its own relative url path and the revision number is appended with a '/rev=' to the url.

This is part of my code for the same.

class WikiPage(MainHandler):
def get(self, url, num):
    num = int(num) if num else 0
    ...    # Display the wiki-entry with that revision number

PAGE = r'(/(?:[a-zA-Z0-9_-]+/?)*)'

app = webapp2.WSGIApplication([
    (r'/wiki' + PAGE + r'(?:/rev=(\d+))?', WikiPage),
    ...  # Other Handlers
], debug = True)

This might not be the best way of doing this (for eg. I can use a query to get the revision numbers but I would like to know how to do it in this way)

When I run the above code, I get an error indicating that the get function takes 3 arguments but is only provided with 2. The url argument is passed correctly but the revision number is not passed. I have also tried naming the group in the raw string as num but it's still not working.

I would appreciate any help as to how I can pass 2 or more parameters to my handlers.

Edit: According to Jamie Gómez's answer, I tried modifying my program but I'm still not able to make it work (I'm getting a 404 error). I also tried it by using unnamed parameters but to no avail. Am I not implementing it correctly?

class WikiPage(MainHandler):
    def get(self, **kw):
        url = kw.get('url')
        num = kw.get('num')
        num = int(num) if num else 0
        ...    # Display the wiki-entry with that revision number

PAGE = r'<url:/(?:[a-zA-Z0-9_-]+/?)*>'

app = webapp2.WSGIApplication([
    (r'/wiki' + PAGE + r'(?:/rev=<num:\d+>)?', WikiPage),
    ...  # Other Handlers
], debug = True)

Upvotes: 1

Views: 898

Answers (3)

Andrea Sindico
Andrea Sindico

Reputation: 7440

why don't you pass the rev as a request parameter like

 /wiki?month=4&year=2015&rev=123

In this way you can get it in your handler in this way

 self.request.get('rev')

Upvotes: 0

Jaime Gomez
Jaime Gomez

Reputation: 7067

The format looks wrong, check out this section of the docs:

The URL template

┌──────────────┬────────────────────────────────────┐
│ Format       │ Example                            │
├──────────────┼────────────────────────────────────┤
│ <name>       │ '/blog/<year>/<month>'             │
├──────────────┼────────────────────────────────────┤
│ <:regex>     │ '/blog/<:\d{4}>/<:\d{2}>'          │
├──────────────┼────────────────────────────────────┤
│ <name:regex> │ '/blog/<year:\d{4}>/<month:\d{2}>' │
└──────────────┴────────────────────────────────────┘ 

Upvotes: 2

Dan Cornilescu
Dan Cornilescu

Reputation: 39814

You could use webapp2.Route(). There's an example here (I'm still working on understanding it entirely, tho): https://github.com/svpino/blog-engine

Upvotes: 0

Related Questions