Nic Meiring
Nic Meiring

Reputation: 882

Adding usernames to the path of the url

I am building an application using webapp2 in Google App Engine. How do I pass the username into the url so that when the profile button is clicked, it takes the user to "/profile/username" where "username" is specific to the user?

My current handlers:

app = webapp2.WSGIApplication([('/', MainPage),
                               ('/signup', Register),
                               ('/login', Login),
                               ('/logout', Logout),
                               ('/profile', Profile)
                               ],
                              debug=True)

the Profile class:

class Profile(BlogHandler):
    def get(self):
        email = self.request.get('email')
        product = self.request.get('product')
        product_list = db.GqlQuery("SELECT * FROM Post ORDER BY created DESC LIMIT 10")
        self.render('profile.html', email = email, product = product, product_list = product_list)

I am trying to send each user to a Profile page that contains information in my database specific to them. Thanks

Upvotes: 0

Views: 93

Answers (3)

Leo
Leo

Reputation: 2072

One possible solution would be to simply have one URL, i.e., /profile. The corresponding handler would render the response with data coming from the logged-in user.

If you really want to have URLs like /profile/username, you could define a route:

app = webapp2.WSGIApplication([('/', MainPage),
                               ('/signup', Register),
                               ('/login', Login), 
                               ('/logout', Logout),
                               ('r/profile/(\w+)', Profile)
                              ],
                              debug=True)

and access the username in your handler:

class Profile(BlogHandler):
    def get(self, username):

But depending on your application, you might want to make sure only the logged-in user has access to its /profile/username by adding a check somewhere in the handler.

Upvotes: 2

Adam Crossland
Adam Crossland

Reputation: 14213

Start by adding a capture group to /profile:

(r'/profile/(\w+)', Profile)

The r before the beginning of the string is important, as it will correctly handle regular expression characters. Otherwise, you'd have to escape the blackslash manually.

\w+ will match one or more alphanumeric characters and the underscore. That should suffice for your usernames, yes?

Then set up your RequestHandler like this:

class Profile(webapp2.RequestHandler):
    def get(self, username):
        # The value captured in the (\w+) part of the URL will automatically
        # be passed in to the username parameter. 
        # Do the rest of my coding to get and render the data.

Upvotes: 0

Sebastian Kreft
Sebastian Kreft

Reputation: 8189

See http://webapp-improved.appspot.com/guide/routing.html

You could have something like

class Profile(BlogHandler):
  def get(self, username):
    ...

app = webapp2.WSGIApplication([('/profile/(\w+)', Profile), ...])

Upvotes: 0

Related Questions