在我的 Google AppEngine 项目中,我试图保存电影的详细信息,包括图像,然后在我的主页上显示图像,但图像没有显示。我正在使用 Python 和 Jinja2。有谁知道为什么?
import webapp2
import jinja2
import os
import datetime
import time
import cgi
from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.api import images
jinja_environment = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
class Movie(db.Model):
name = db.StringProperty()
release_date = db.StringProperty()
duration = db.StringProperty()
director = db.StringProperty()
actors = db.StringProperty(multiline=True)
summary = db.StringProperty(multiline=True)
picture = db.BlobProperty()
trailer = db.StringProperty(multiline=True)
date = db.DateTimeProperty(auto_now_add=True)
class Image(webapp2.RequestHandler):
def get(self):
movie = db.get(self.request.get('img_id'))
if movie.picture:
img = images.Image(movie.picture)
self.response.headers['Content-Type'] = 'image/jpeg'
self.response.out.write(img)
else:
self.response.out.write("No Image")
class Main(webapp2.RequestHandler):
def get(self):
movies = db.GqlQuery("SELECT * "
"FROM Movie "
"ORDER BY release_date DESC LIMIT 5")
template_values = { 'movies': movies}
template = jinja_environment.get_template('home_page.html')
self.response.out.write(template.render(template_values))
class Admin(webapp2.RequestHandler):
def get(self):
template_values = {}
template = jinja_environment.get_template('admin.html')
self.response.out.write(template.render(template_values))
def post(self):
name = self.request.get('name')
release_date = self.request.get('release_date')
c = time.strptime(release_date,"%m/%d/%Y")
release_date = time.strftime("%m%d%Y",c)
duration = self.request.get('duration')
director = self.request.get('director')
actors = self.request.get('actors')
summary = self.request.get('summary')
picture = self.request.get('img')
trailer = self.request.get('trailer')
movie = Movie(key_name = name)
movie.release_date = release_date
movie.duration = duration
movie.director = director
movie.actors = actors
movie.summary = summary
movie.picture = db.Blob(str(picture))
movie.trailer = trailer
movie.put()
self.redirect('/')
app = webapp2.WSGIApplication([('/admin', Admin), ('/', Main)], debug=True)
这是 home_page.html 的 HTML:
<html>
<body>
<table>
<th>Top Movies</th>
{% for movie in movies %}
<tr>
<td>{{movie.key().name() }}  </td>
</tr>
<tr>
<td><a href = "/movie_data?movie_key = {{movie.key() }}"> <img src = "img?img_id={{movie.key()}}">       </td>
</tr>
{% endfor %}
<a href = "/admin">admin</a>
</table>
</body>
</html>
这是 admin.html:
html>
<body dir="ltr">
<form action="/admin" method="post">
<table>
<th colspan="2"><h1>Add New Movie</h1><br/></th>
<tr><td>Movie Name:</td><td><input type="text" name="name"/><br/></td></tr>
<tr><td>Release Date (MM/DD/YYYY):</td><td><input type="text" name="release_date" /><br/></td></tr>
<tr><td>Duration (minutes):</td><td><input type="text" name="duration" /><br/></td></tr>
<tr><td>Director:</td><td><input type="text" name="director" /><br/></td></tr>
<tr><td>Main Actors:</td><td><textarea name="actors" rows = "5" cols = "20"></textarea><br/></td></tr>
<tr><td>Summary:</td><td><textarea name="summary" rows = "5" cols = "20"></textarea><br/></td></tr>
<tr><td>Upload Picture</td><td><input type="file" name="img"/><br/></td></tr>
<tr><td>Trailer Code:</td><td><textarea name="trailer" rows = "5" cols = "20"></textarea><br/></td></tr>
<tr><td><br/><input type="submit" />       <input type="reset" /></td></tr>
</table>
</form>
</body>
</html>