suitegamer
suitegamer

Reputation: 421

Unique User Profiles via URL

Alright, i've asked this question in the past. However, when I asked it, I had a limited knowledge of python and app engine. This is most likely why I failed to implement this in the past. I'm attempting to get each profile to be unique based on the username.

Any who, before I dive in, i'll throw my profile handler up that only deals with currently logged in users at the moment:

class Profile(MainHandler):
    def get(self):
        if self.user:
            current_user = str(self.user.name)
            key = ''
            imgs  = db.GqlQuery("select * from Profile_Images WHERE name =:1", current_user)
            team_name  = db.GqlQuery("select * from Teams WHERE name =:1", current_user)
            team_images  = db.GqlQuery("select * from Teamimg WHERE user =:1", current_user)
        for clan in team_name:
                name1 = clan.team_name_anycase
        for image in team_images:
            team_imagee = image.key()
        if self.user:
            for img in imgs:
                key = img.key()
            self.render('profile.html', team_img = team_imagee, team_name = name1, profile_image = key, username = self.user.name, email = self.user.email, firstname = self.user.first_name, last_name = self.user.last_name, country = self.user.country)
        else:
            self.redirect('/register')

This handler is mapped via ('profile', Profile).

Any who, what I understand thus far is that I need to pass the username through the URL and into the profile handler which then uses that username as identifier for pulling data from the db.

What I saw posted on stackoverflow was ('profile/<profile_id>', Profile). And I have been messing around with that for a bit, but it seems that the trailing username (ex.localhost:8080/profile/admin) is getting a 404 error. I would assume that either my mapping is failing, or the variable (i.e. the username) is failing to interact with the profile handler.

Could someone please help me out here? I was sure I had it, and it failed.

YAML file:

application: suitegamer
version: 1
runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /static
  static_dir: static

- url: /img
  static_dir: img

- url: /.*
  script: main.app

libraries:
- name: jinja2
  version: latest

- name: PIL
  version: "1.1.7"

MainHandler:

class MainPage(MainHandler):
    def get(self):
    if self.user:
            self.render('index.html', username = self.user.name, firstname = self.user.first_name)
        else:
            self.render('index.html')

    def post(self):
    username = self.request.get('username').lower()
        password = self.request.get('password')

        u = User.login(username, password)
        if u:
            self.login(u)
            self.redirect('/news_page')
        else:
            msg = 'Invalid login.'
            self.render('login.html', error = msg)

Mapping:

app = webapp2.WSGIApplication([('/', MainPage),
                               ('/logout', Logout),
                               ('/img', GetImage),
                               ('/register', Register),
                               ('/welcome', Welcome),
                               ('/news_page', News_Page),
                               ('/profile', Profile),
                               ('/edit_profile', Edit_Profile),
                               ('/change_profile_image', Change_Profile_Image),
                               ('/found_a_team', Found_Team),
                               ('/team_main', Team_Main),
                               ('/edit_team_main', Edit_Team_Main),
                               ('/edit_team_image', Edit_Team_Image)],
                              debug=True)

Testing Profile Handler:

class Profile(MainHandler):
    def get(self, profile_id):
        profile_id = 'admin'
        if self.user:
            key = ''
            imgs  = db.GqlQuery("select * from Profile_Images WHERE name =:1", profile_id)
            team_name  = db.GqlQuery("select * from Teams WHERE name =:1", profile_id)
            team_images  = db.GqlQuery("select * from Teamimg WHERE user =:1", profile_id)
        for clan in team_name:
                name1 = clan.team_name_anycase
        for image in team_images:
            team_imagee = image.key()
        if self.user:
            for img in imgs:
                key = img.key()
            self.render('profile.html', team_img = team_imagee, team_name = name1, profile_image = key, username = self.user.name, email = self.user.email, firstname = self.user.first_name, last_name = self.user.last_name, country = self.user.country)
        else:
            self.redirect('/register')

Image Handler:

class GetImage(MainHandler):
    def get(self):
        img = db.get(self.request.get("entity_id"))
        self.response.out.write(img.image)

Maps to ('/img', GetImage)

Upvotes: 0

Views: 236

Answers (1)

aschmid00
aschmid00

Reputation: 7158

the mapping should look like this:

('/profile/(.*)/?', Profile)

and your Handlers get function should take a parameter which will be the profile id/name

class Profile(MainHandler):
    def get(self, profile_id):
        # do something with the profile id...

i think you should give the docs a deeper look.
for example here: https://developers.google.com/appengine/docs/python/tools/webapp/running

Upvotes: 1

Related Questions