下午好,

我目前正试图在Google AppEngine中构建一些非常简单的东西。目标是构建一个简单的照片共享应用程序,它将连接回我的iPhone应用程序。这对Python和Objective-C来说都是一次学习经历。

(我已经很长时间以来一直是PHP程序员。)

目标,创建如下所示的URL: / img / {{model.key.id}}

问题在于,无论我如何处理python脚本,我最终都会遇到错误,或者只是在我的模板页面上显示没有任何东西显示在FOR语句中。

我的App.yaml文件:

application: randomwebappname
version: 1
runtime: python
api_version: 1

handlers:
- url: /media
  static_dir: media

- url: /b/.*
  script: beta.py
  login: required

- url: /.*
  script: main.py

我的模型(在beta.py内):

class Photo(db.Model):
    author = db.StringProperty()
    title = db.StringProperty()
    slugline = db.StringProperty()
    content = db.StringProperty(multiline=True)
    coordinates = db.StringProperty()
    avatar = db.BlobProperty()
    date = db.DateTimeProperty(auto_now_add=True)

我的观看图片页面的课程:

class ViewPage(webapp.RequestHandler):
def get(self, id):


template_values = {
    'image': image,
}

path = os.path.join(os.path.dirname(__file__), 'templates/view.html')
self.response.out.write(template.render(path, template_values))

我在课堂上尝试了以下所有内容,但都以失败告终。我在URL中使用key,key.name和key.id尝试了它们:

photos = db.Query(Photo).filter('key', slug).fetch(limit=1)

photos = Photo.get_by_key_name(id)

photos = Photo.get_by_key_name(key)

key = db.Key.from_path('Photo', id) 

photos = db.GqlQuery("SELECT * FROM Photo WHERE __key__ = :key", key=key)

photos = db.get(photo_key)

photos = self.request.get("id")

我的网址:

application = webapp.WSGIApplication([
    ('/b/', HomePage),
    ('/b/upload', UploadPage),
    ('/b/add', MainPage),
    ('/b/img', Image),
    ('/b/img/([-\w]+)', ViewPage),
    ('/b/submit', Submission)
], debug=True)

模板查询:

{% for photo in photos %}
<img alt="" title="" src="img?img_id={{ photo.key }}" alt="main image" />
{% endfor %}

这似乎是非常简单的东西,我知道我错过了什么,但我只是不确定它在哪里。我会用PHP编写这个,但我喜欢AppEngine的概念,我上面已经说过,这是一个很好的Python学习经验。

作为旁注,此应用程序可在网站的主页上运行。我只是有一个GQL查询,它输出图像很好,当我尝试转到/ img / id页面时它就失败了。

任何建议人(和女孩)?提前谢谢!

更新#1
根据Jonathan的要求,以下是Image类:

class Image (webapp.RequestHandler):
    def get(self):
        photo = db.get(self.request.get("img_id"))
        if photo.avatar:
            self.response.headers['Content-Type'] = "image/png"
            self.response.out.write(photo.avatar)
        else:
            self.response.out.write("No image")

另外,在发布这个之后,我意识到这是问题的一部分,因为我试图将/ img作为实际图像和/ img /来显示视图页面。我已经改变了这个,现在有一个工作模型。但它基于Key而不是key.id:

<强> URL:

('/b/i', ViewPage)

新的ViewPage类:

class ViewPage(webapp.RequestHandler):
    def get(self):
    image = db.get(self.request.get("id"))  

    template_values = {
        'image': image,
    }

    path = os.path.join(os.path.dirname(__file__), 'templates/view.html')
    self.response.out.write(template.render(path, template_values))

所以......要进入“查看”页面(包括评论等),您现在必须转到以下URL: / b / i?img_id = {{image.key}}

至少该页面现在正在运行,但我更希望如上所述让页面看起来如下所示: / b / img / {{image.key.id}}

更新#2:更新了ViewPage类以及网址:

class ViewPageV2(webapp.RequestHandler):
    def get(self, id):
    images = [ db.get(id) ]

    template_values = {
        'image': image,
    }

    path = os.path.join(os.path.dirname(__file__), 'templates/view.html')
    self.response.out.write(template.render(path, template_values))

新网址:     ('/ b / image /([ - \ w] +)',ViewPageV2),

以下是两个屏幕截图,其中一个是id为“1”的证据。现有的和当前出现的错误。

替代文字http://img215.imageshack.us/img215/9123/screenshot20091130at937 .PNG

alt text http://img215.imageshack.us/img215/2207/screenshot20091130at938 .PNG

再次,谢谢大家!

有帮助吗?

解决方案

一种简单的方法来抓住它们:

photos = Photo.gql('ORDER BY __key__')

有关详情,请参阅按键查询在App Engine文档中。

您是否使用预定义的密钥存储照片

photo = Photo(key_name="xzy123")
photo.put()

然后您可以在ViewPage中检索它:

photos = [ Photo(key_name="%s" % id) ]

请参阅使用密钥获取实体

最后,要根据其appengine指定的密钥检索照片,并假设此密钥存在于URL中(例如 http:// host / b / img / ahByY。 .. ,所以更改模板以生成此表单的URL),使用

class ViewPage(webapp.RequestHandler):
  def get(self, id):
    photos = [ db.get(id) ]
    ...
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top