为每个用户python创建独特的配置文件页面 [英] create unique profile page for each user python

查看:174
本文介绍了为每个用户python创建独特的配置文件页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这可能是一个愚蠢的解决方案,但我有一个数千名用户的列表,并且是正确的现在他们只能访问他们自己的配置文件页面并且必须登录才能执行此操作。我想为每个用户提供他们的个人资料页面的唯一URL,我想知道如何去做。

  class ProfilePage 
userlist = GQL查询我不确定这是否可行,但可以这样做吗?返回系统中的所有用户
user = users.get_by_id()
用户列表中的用户:
id = user.federated_id

posts = GQL查询返回该用户的所有帖子

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

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

个人资料页面的我的HTML只显示用户的姓名,然后显示他们最近的帖子。



更新:



因此,这里是我当前的代码,但我刚刚得到一个404错误:
$ b

  class ProfilePage(webapp2。 RequestHandler):
def get(self,profile_id):
user = User.get_by_id(profile_id)
#profile_id =一些唯一字段
如果用户:
#获取该用户的所有帖子并呈现....
theid = user.theid
personalposts = db.GqlQuery(select * from Post where theid =:1 order通过创建desc限制30,theid)
else:
personalposts = None
全局访问
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 b
$ b 更新:
我检索的ID不是它们的OpenID URL,而是每个用户给出的特定于应用程序的ID,因此这就是正确使用

解决方案

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

这篇关于为每个用户python创建独特的配置文件页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆