【问题标题】:create unique profile page for each user python为每个用户 python 创建唯一的个人资料页面
【发布时间】:2012-06-30 07:58:41
【问题描述】:

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

这可能是一个愚蠢的解决方案,但我有一个包含数千名用户的列表,现在他们只能访问自己的个人资料页面并且必须登录才能访问。我想为每个用户的个人资料页面提供一个唯一的 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,因此使用正确

【问题讨论】:

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


【解决方案1】:

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

【讨论】:

  • 并且使用该代码,当其他用户(可能未登录)访问时,唯一的个人资料网站是否会保持不变?
  • 是的,它使用 URL 中的参数作为要加载其个人资料页面的标识符,而不是当前登录的用户
  • 我刚刚更新了代码,正如我所说的,我收到了 404 错误。有什么想法吗?
  • 从您重复发布的问题中,我了解到您没有使用正确的正则表达式,此外,您的用户模型密钥名称是否为他们的 OpenID 登录 URL?我不希望您应该查看用户实体的 id/key 名称并尝试在您的 URL 中使用它,而不是他们的 OpenID URL。请发布您的用户模型代码并将您的正则表达式更新为我给出的示例(您可以使用&lt;profile_id&gt;,因为它成为一个命名参数)。
  • 啊,你是对的,我试图使用 OpenID URL,但实际 ID 值只是一个随机数,感谢提示 是一个实际参数。感谢您的帮助!
猜你喜欢
  • 2018-12-09
  • 1970-01-01
  • 1970-01-01
  • 2015-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-21
  • 2016-10-30
相关资源
最近更新 更多