ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-10-09 21:57:35  阅读:227  来源: 互联网

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


我正在使用带有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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有