I’ve created quite a beefy app. I have a profile page system implemented. Now, I am a pretty big ‘noob’ when it comes to programming and app engine, so be gentle here, (please). So I know that you can pass parameters in to the url example, /profile/. I’m just not completely sure how to implement that into my code or the thought process behind it.
This would be my database:
class User(db.Model):
name = db.StringProperty(required = True)
pw_hash = db.StringProperty(required = True)
email = db.StringProperty(required = True)
This is my profile handler:
class Profile(MainHandler):
def get(self):
if self.user:
current_user = str(self.user.name)
key='ag5kZXZ-c3VpdGVnYW1lcnINCxIHSW1hZ2VkYhglDA '
imgs = db.GqlQuery("select * from Imagedb WHERE name =:1", current_user)
for img in imgs:
key = img.key() # this is the key
self.render('profile.html', profile_image = key ,username = self.user.name, email = self.user.email, first_name = self.user.first_name, last_name = self.user.last_name, country = self.user.country, prov_state = self.user.prov_state, city_town = self.user.city_town)
else:
self.redirect('/register')
The profile has a display picture system implemented at the moment as well. My goal here is to basically build what you would call a social network. AKA, blog on each profile, and a message system, along with some other systems.
I appreciate any and all help. Even if they are just your thoughts/opinions. I have my site up loadable to a domain, so it would be possible to show you, but I would really like to keep it under wraps until its done :P.
Thanks very much.
EDIT:
This is the solution I saw posted, however I not sure how to go about implementing it.
class User(ndb.Model):
unique_identifier = ndb.StringProperty()
...
class ProfilePage(webapp2.RequestHandler):
def get(self, profile_id):
#profile_id = key name of user
user = User.get_by_id(profile_id)
#profile_id = some unique field
#user = User.query(User.unique_identifier == profile_id).get()
if user:
#Get all posts for that user and render....
app = webapp2.WSGIApplication([('/', MainPage),
('/profile/<profile_id>', ProfilePage),])
You’re looking to make the profile_id part of the URL and you want that ID passed to your request handler. This change to your application definition is defining the mapping between ‘routes’ and RequestHandler objects:
Typically these mappings are a URL or URL regex with some syntax/convention to identify variables.
In this case
'/profile/<profile_id>'is aroute templatethat uses the nameprofile_idto match whatever is passed in the second path segment.Any matches are passed as positional arguments to your RequestHandler functions. This is handled by the framework, you just need to add arguments for each
<name[:regex]>block in your route.The reason that the posted solution recommends adding a unique_identifier field to the User model is store the canonical user_id / profile_id. Say you just use an integer to represent the unique user identity.
Your route template could be changed to only match integers, like
'/profile/<user_id:\d+>/'and the url for a user would be something like/profile/1024/.