且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为每个用户python创建独特的配置文件页面

更新时间:2022-10-14 20:25:30

一个简单的方法可以为每个用户分配一个唯一的URL标识符(或使用他们的密钥名称),这样您可以通过ID查询用户,或者根据唯一的URL标识符属性执行查询。如果您愿意,您也可以使用他们的federated_id。



示例:

  class User(ndb.Model):
unique_identifier = ndb.StringProperty()
...
$ b $ class ProfilePage(webapp2.RequestHandler):
def get( self,profile_id):
#profile_id =用户的密钥名称
user = User.get_by_id(profile_id)
#profile_id =某些唯一字段
#user = User.query(User .unique_identifier == profile_id).get()
如果用户:
#获取该用户的所有帖子并呈现....


app = webapp2。 WSGIApplication([('/',MainPage),
('/ profile /< profile_id&',ProfilePage),])


I am using google app engine in python with a Jinja2 template engine.

This may be a silly solution but I have a list of a few thousand users and right now they can only access their own profile pages and have to be logged in to do it. I would like to give every user a unique URL for their profile page and I am wondering how to do it. I am not sure if this would work but could something like this be feasible?

class ProfilePage
    userlist = GQL query to return all users in the system
    user = users.get_by_id()
    for user in userlist:
        id = user.federated_id

        posts = GQL query to return all posts by that user

        self.render('/profile/id', posts=posts)

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

My HTML for the profile page just displays the user's name and then displays all of their recent posts.

Update:

So here is my current code but I am just getting a 404 error:

class ProfilePage(webapp2.RequestHandler):
  def get(self, profile_id):
    user = User.get_by_id(profile_id)
    #profile_id = some unique field
    if user:
       #Get all posts for that user and render....
       theid = user.theid
       personalposts = db.GqlQuery("select * from Post where theid =:1 order by created desc limit 30", theid)
    else:
        personalposts = None
    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)

How can I test it out I tried just entering www.url.com/profile/https://www.google.com/accounts/o8/id?id=AItOawlILoSKGNwU5RuTiRtXug1l8raLEv5-mZg

Update: The ID I was retrieving was not their OpenID URL but rather a app specific id that each user is given and thus that is the correct to use

An easy way to do this would be to assign a unique URL identifier to each user (or use their key name), that way you can query the user by their ID or do a query based on a unique URL identifier property. You can also use their federated_id if you wanted.

Example:

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),])