编程语言
首页 > 编程语言> > 为每个用户python创建唯一的配置文件页面

为每个用户python创建唯一的配置文件页面

作者:互联网

我正在使用带有Jinja2模板引擎的python中的google app引擎.

这可能是一个愚蠢的解决方案,但是我有一个数千用户的列表,现在他们只能访问自己的个人资料页面,并且必须登录才能执行此操作.我想为每个用户的个人资料页面提供一个唯一的URL,我想知道如何做.我不确定这是否行得通,但是这样的事情可行吗?

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

我的个人资料页面HTML仅显示用户名,然后显示其所有最新帖子.

更新:

所以这是我当前的代码,但我刚遇到404错误:

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)

我如何才能尝试我刚刚输入www.url.com/profile/https://www.google.com/accounts/o8/id?id=AItOawlILoSKGNwU5RuTiRtXug1l8raLEv5-mZg

更新:
我检索的ID不是他们的OpenID URL,而是每个用户都被赋予的应用程序特定ID,因此可以正确使用

解决方法:

一种简单的方法是为每个用户分配一个唯一的URL标识符(或使用其键名),这样您就可以通过用户的ID查询该用户或基于唯一的URL标识符属性进行查询.如果需要,还可以使用其federated_id.

例:

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

标签:html,python,google-app-engine,jinja2,user-profile
来源: https://codeday.me/bug/20191009/1882093.html