Python 为每个用户创建唯一的配置文件页面
我正在使用python中的GoogleAppEngine和Jinja2模板引擎 这可能是一个愚蠢的解决方案,但我有一个数千名用户的列表,现在他们只能访问自己的个人资料页面,并且必须登录才能这样做。我想为每个用户的个人资料页面提供一个唯一的URL,我想知道如何做到这一点。我不确定这是否可行,但类似的事情是否可行Python 为每个用户创建唯一的配置文件页面,python,html,google-app-engine,jinja2,user-profile,Python,Html,Google App Engine,Jinja2,User Profile,我正在使用python中的GoogleAppEngine和Jinja2模板引擎 这可能是一个愚蠢的解决方案,但我有一个数千名用户的列表,现在他们只能访问自己的个人资料页面,并且必须登录才能这样做。我想为每个用户的个人资料页面提供一个唯一的URL,我想知道如何做到这一点。我不确定这是否可行,但类似的事情是否可行 class ProfilePage userlist = GQL query to return all users in the system user = users.
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),])
个人资料页面的MyHTML只显示用户的姓名,然后显示他们最近发表的所有文章
更新:
这是我当前的代码,但我刚刚得到一个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标识符属性执行查询。如果需要,您还可以使用它们的联邦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),])
类用户(ndb.Model):
唯一_标识符=ndb.StringProperty()
...
类配置文件页(webapp2.RequestHandler):
def get(自我,配置文件id):
#profile\u id=用户的密钥名
user=user.get\u by\u id(profile\u id)
#profile\u id=某个唯一字段
#user=user.query(user.unique\u identifier==profile\u id.get())
如果用户:
#获取该用户的所有帖子并呈现。。。。
app=webapp2.WSGIApplication([(“/”,主页),
(“/profile/”,ProfilePage),])
当另一个用户(可能未登录)访问该唯一配置文件站点时,该站点是否会保持不变?是的,它使用URL中的参数作为要加载其配置文件页面的标识符,而不是当前登录的用户。我刚刚更新了代码,正如我所说,我收到了404错误。有什么想法吗?从你重复的问题中我推断你没有使用正确的正则表达式,还有,你的用户模型密钥名是他们的OpenID登录URL吗?我希望不是这样,所以您应该查看用户实体的id/密钥名称,并尝试在URL中使用它,而不是在其OpenID URL中使用它。请发布您的用户模型代码,并将您的正则表达式更新到我给出的示例中(您可以使用
,因为它成为命名参数)。啊,您是对的,我尝试使用OpenID URL,但实际ID值只是一个随机数,感谢您的提示,这是一个实际参数。谢谢你的帮助!看看我在网站上发布的类似问题的答案