可能的重复项:
为每个用户 Python 创建唯一的配置文件页面
我正在使用带有python和jinja2的谷歌appengine,并试图为我的应用程序中的每个用户提供一个指向其个人资料页面的唯一URL,任何人都可以在不登录的情况下访问该URL。 这是我到目前为止的代码:
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)
app = webapp2.WSGIApplication([('/', MainPage),
('/profile/([0-9]+)', ProfilePage),])
当我尝试测试它时,它只会给我一个 404 错误。 我想如果代码是正确的,我可能使用了错误的测试 URL。 例如,如果这是他们的 OpenID ID:我该如何测试它我尝试只输入 www.url.com/profile/https://www.google.com/accounts/o8/id?id=AItOawlILoSKGNwU5RuTiRtXug1l8raLE45g-56 只是 id="这部分" 是我放的,所以我会有:
url = www.url.com/profile/AItOawlILoSKGNwU5RuTiRtXug1l8raLE45g-56
这就是我尝试过的,但它并没有完全奏效。 提前感谢您的帮助!
试试这个正则表达式:
r'^/profile/w*?-(d+)$'
虽然我也必须告诉你,这是一个非常糟糕的主意!
您正在使用的 url(www.url.com/profile/AItOawlILoSKGNwU5RuTiRtXug1l8raLE45g-56) URL 的最后一部分是实体的完整键,而在代码中使用实体 ID 加载它(使用 get_by_id() )。