【问题标题】:Deriving Key() from get_by_id()从 get_by_id() 派生 Key()
【发布时间】:2012-05-27 23:56:27
【问题描述】:

我已经进行了用户注册,其中包括(姓名、密码、电子邮件、电话号码),现在尝试将图像也包含在 Google App Engine 数据存储中。一切正常,只是图像上传和调整大小很麻烦。但是,我现在已经让它工作了。

基本上有三个处理程序

  • 第一个注册页面处理程序(信息测试和重定向到用户页面)

"""从注册页面获取所有请求并存储在数据库中"""

userinfo.put()
u_id = userinfo.key().id()

"""创建 cookie 和哈希值,其中值为 u_id"""

self.redirect(/userpage)

  • 其次是用户页面处理程序(验证在注册处理程序中创建的cookie,如果哈希cookie有效则重定向到用户页面,否则再次重定向到注册页面)
user_id = Users.get_by_id(u_id)
user_key = ?  # how can I get key of user_id

self.render("user.html", user =user_id.user, avatar = user_id.avatar)

  • 第三是图像处理程序
class disp_image(webapp.RequestHandler):
  def get(self):
      key = self.request.get('key')
      image = Users.get(key)
      self.response.headers['Content-Type'] = "image/png"
      return self.response.out.write(image.avatar)

模板

<img src="/disp?key={{avatar}}" />

我已经解决了这个问题,进行了修订,以便更清楚地了解谁之前对我的问题投了反对票。

【问题讨论】:

  • 因风格而投反对票...我想阅读更多文字,描述您问题的主题/背景。
  • @thchand:编辑您的问题以将其变成一个全新的问题真的很酷。如果您有新问题要问,请在新问题中提出。
  • @Adam Crossland- 感谢您的建议

标签: python google-app-engine key


【解决方案1】:

user_key = user_id.key()

但是为什么你的头在旋转?你到底想完成什么?

【讨论】:

    【解决方案2】:

    我想你的问题是理解,key,key_name 和 id?你要仔细阅读类的文档db.Key

    https://developers.google.com/appengine/docs/python/datastore/keyclass

    key = userinfo.key() # Returns an instance of the class `db.Key`
    if key.id(): # That means the key use a numeric ID
      id = key.id()
    else: # That means you can not have an ID in numeric and in string
      key_name = key.name() # That means the key use a string ID
    

    正如您在文档中看到的,当您想创建一个对象 db.Key 时,您可以使用 Key.from_path(kind, id_or_name, parent=none, namespace=None, **kwds) 方法,此方法使用字符串 长字符串作为 ID。 p>

    id,指定为字符串或长整数。不能是数字 0。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多