Search code examples
pythongoogle-app-engineurl-mapping

404 error on unique page creation with google app engine


I asked a similar question here: create permenant unique links based on a user ID but couldn't quite get an answer. I am trying to give every user on my site a unique profile page. I have it mostly set up but I keep getting a 404 error. Now I am not sure if it is a problem with my handler or just the whole way I am doing it.

Here is my app code:

app = webapp2.WSGIApplication([('/', MainPage),
                               (r'/profile/(.+)', ProfilePage)])

and here is my ProfilePage handler class:

class ProfilePage(webapp2.RequestHandler):
    def get(self, profile_id):
        profileowner = User.get_by_id(profile_id)

        if profileowner:
            #Get all posts for that user and render....
            #theid is their federated_id 
            theid = profileowner.theid
            personalposts = db.GqlQuery("select * from Post where theid =:1 order by created desc limit 30", theid)

            #I collect this so that I can have their username in the top of the page
            global visits
            logout = users.create_logout_url(self.request.uri)
            currentuser = users.get_current_user()
            self.render('profile.html', user = currentuser, visits = visits, logout=logout, personalposts=personalposts)
        else:
            self.redirect("/")

For an ID of 1201, which I found in the datastore viewer for a use, I have been testing it by typing in www.url.com/profile/1201 and that is when I get the 404 error.

Update: It now is redirecting me to the main page with Amber's suggested change.

Now when I change this line:

profileowner = User.get_by_id(profile_id)

to this:

profileowner = User.get_by_id(17001)

it goes through correctly so I am guessing that that line is not correctly getting the profile_id from the URL


Solution

  • r'/profile/<profile_id>'
    

    is not a valid regular expression. You probably want something like this instead:

    r'/profile/(.+)'