0

我无法让我的祖先查询显示与存储在数据存储中的Tom 的照片相关联的链接。即使数据存储中有多个链接,数据存储也不会显示任何内容。任何帮助将不胜感激。提前致谢。

主文件

import webapp2
import jinja2
import os

from google.appengine.ext import db

jinja_environment = jinja2.Environment(
    loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))

class MainPage(webapp2.RequestHandler):
    def get(self):          

        tom = Person(key_name='Tom')

        wedding_photo = Photo(parent=tom)
        wedding_photo.image_url='http://pinterest.com/pin/200691727117011230/'
        wedding_photo.put()

        baby_photo = Photo(parent=tom)
        baby_photo.image_url='http://pinterest.com/pin/518828819542052681/'
        baby_photo.put()

        dance_photo = Photo(parent=tom)
        dance_photo.image_url='http://pinterest.com/pin/257197828689352707/'
        dance_photo.put()

        dog_photo = Photo()
        dog_photo.image_url='http://pinterest.com/pin/279575089339614779/'
        dog_photo.put()


        photo_query = Photo.all()
        photo_query.ancestor(tom)

        message = "Photos"

        template_values = {
            'message': message,
            'photo_query': photo_query,     
        }       

        template = jinja_environment.get_template('index.html')
        self.response.out.write(template.render(template_values))


class PhotoStore(webapp2.RequestHandler):
    def post(self):
        photo = Photo()
        photo.image_url = self.request.get('image_url')     
        photo.put() 
        self.redirect('/')      


class Person(db.Model):
    name = db.StringProperty()


class Photo(db.Model):
    image_url = db.StringProperty()

app = webapp2.WSGIApplication([('/', MainPage),
                                ('/new_photo',PhotoStore)], debug=True)

索引.html

<html>
    <body>                      
        <form action="/new_photo" method="post">            
            <label for="photo">Photo</label>
            <div><textarea name="image_url" rows="1" cols="60" id="image_url"></textarea></div>         
            <input type="submit" value="Submit">
        </form>
        <hr></hr>
        <div><b>{{ message }}</b></div>             
        <b>{% for p in photo_query.run(limit=5): %}</b>
        <div>{{ p }}</div>
        <b>{% endfor %}</b>
    </body>
</html>
4

1 回答 1

2

如果打开生成的 html 源代码,您会看到什么?我假设代码{{ p }}只显示Photo对象的字符串表示,这导致显示的字符串<__main__.Photo object at 0xfcbd9ef0>被解释为 HTML 标记,并且不会显示在呈现的 HTML 中。

于 2013-03-24T04:03:24.580 回答