<a href="/profile/{{user.key.id()}}">Profile</a>
“user” is a variable that I am passing from my controller. By using
{{user.key.id()}}
app = webapp2.WSGIApplication([('/', MainPage),
('/profile/([0-9]+)', Profile),
],
debug=True)
([0-9]+)
which matches any number of integers. As well, by putting the regex inside the parentheses, I can pass the user id to my profile class as an argument.
class Profile(Handler):
def get(self, profile_id):
key = ndb.Key('User', int(profile_id), parent=users_key())
user = key.get()
self.render("profile.html", profile_user=user.name)
I can use the profile_id to get the key of the post and use the key to get the user entity.
Since I want some information of a user on the user profile page, I am passing the name of the user to display it on the profile page.
My user entity has a “name” property so I can simply pass the name of the user and render my profile template with the variable. My profile.html file includes
{{profile_user}}
Once you render the template, you will see the name being displayed on the profile page.
That’s it! Happy coding 🙂