【问题标题】:create permenant unique links based on a user ID [duplicate]根据用户 ID 创建永久唯一链接 [重复]
【发布时间】:2012-06-30 16:29:32
【问题描述】:

可能重复:
create unique profile page for each user python

我正在使用带有 python 和 jinja2 的 google 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:我如何测试它p>

url = www.url.com/profile/AItOawlILoSKGNwU5RuTiRtXug1l8raLE45g-56

这就是我尝试过的方法,但效果不佳。提前感谢您的帮助!

【问题讨论】:

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


    【解决方案1】:

    试试这个正则表达式:

    r'^/profile/\w*?-(\d+)$'
    

    虽然我也必须告诉你这是一个非常糟糕的主意!

    【讨论】:

    • 就因为任何人都可以放任何东西就那么糟糕吗?
    • 您使用的是参数化查询,所以这不是问题。只是不要在其他任何地方使用该值(例如,不要将其回显到页面中)。
    【解决方案2】:

    您正在使用的网址 (www.url.com/profile/AItOawlILoSKGNwU5RuTiRtXug1l8raLE45g-56) 网址的最后一部分是实体的完整密钥,而在您使用的代码中实体 id 加载它(使用 get_by_id() )。

    【讨论】:

    • 这不是一个秘密 URL,我希望它是公开的。这就是它的全部原因,以便每个人都可以看到它
    • 我在哪里做的?我不太确定这意味着什么......
    猜你喜欢
    • 2012-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多