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

时间:2012-06-28 18:25:27

标签: python html google-app-engine jinja2 user-profile

我在python中使用带有Jinja2模板引擎的谷歌应用引擎。

这可能是一个愚蠢的解决方案,但我有几千个用户的列表,现在他们只能访问自己的个人资料页面,必须登录才能这样做。我想为每个用户提供他们个人资料页面的唯一网址,我想知道如何做到这一点。我不确定这是否可行,但这样的事情是否可行?

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,因此这是正确的使用

1 个答案:

答案 0 :(得分:2)

一种简单的方法是为每个用户分配一个唯一的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),])