在我的项目中(使用 Python、Html 和 Jinja2),我有一个带有电影图片的主页,每张图片都指向带有电影数据的特定页面。当我尝试使用电影数据呈现页面时,出现以下错误:
Traceback (most recent call last):
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line
1536, in __call__
rv = self.handle_exception(request, response, e)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line
1530, in __call__
rv = self.router.dispatch(request, response)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line
1278, in default_dispatcher
return route.handler_adapter(request, response)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line
1102, in __call__
return handler.dispatch()
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 572,
in dispatch
return self.handle_exception(e, self.app.debug)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 570,
in dispatch
return method(*args, **kwargs)
File "C:\Users\User\Desktop\imovie-good\main.py", line 67, in get
movie_data = db.get(self.request.get('movie_key'))
File "C:\Program Files
(x86)\Google\google_appengine\google\appengine\ext\db\__init__.py", line 1537, in get
return get_async(keys, **kwargs).get_result()
File "C:\Program Files
(x86)\Google\google_appengine\google\appengine\ext\db\__init__.py", line 1496, in
get_async
keys, multiple = datastore.NormalizeAndTypeCheckKeys(keys)
File "C:\Program Files
(x86)\Google\google_appengine\google\appengine\api\datastore.py", line 178, in
NormalizeAndTypeCheckKeys
keys = [_GetCompleteKeyOrError(key) for key in keys]
File "C:\Program Files
(x86)\Google\google_appengine\google\appengine\api\datastore.py", line 2739, in
_GetCompleteKeyOrError
key = Key(arg)
File "C:\Program Files
(x86)\Google\google_appengine\google\appengine\api\datastore_types.py", line 378, in
__init__
raise datastore_errors.BadKeyError('Invalid string key %s.' % encoded)
BadKeyError: Invalid string key .
这是我的python代码:
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 Movie_Data(webapp2.RequestHandler):
def get(self):
movie_key = self.request.get('movie_key')
movie_data = Movie.get(movie_key)
template_values = {'movie_data': movie_data}
template = jinja_environment.get_template('movie_data.html')
self.response.out.write(template.render(template_values))
class Image(webapp2.RequestHandler):
def get(self):
movie = db.get(self.request.get('img_id'))
if movie.picture:
self.response.headers['Content-Type'] = 'image/jpeg'
self.response.out.write(movie.picture)
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(picture)
movie.trailer = trailer
movie.put()
self.redirect('/')
app = webapp2.WSGIApplication([('/admin', Admin), ('/', Main), ('/img', Image), ('/movie_data', Movie_Data)], debug=True)
这是我的主页 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>
这是我的 movie_data html 代码:
<html>
<body>
<div>
<table>
<tr>
<td rowspan ="6">{{ movie_data.picture }}</td>
<td> Movie Name: {{movie_data.name}}</td>
</tr>
<tr>
<td> Release Date: {{movie_data.release_date}}</td>
</tr>
<tr>
<td> Duration (minutes): {{movie_data.duration}}</td>
</tr>
<tr>
<td> Director: {{movie_data.director}}</td>
</tr>
<tr>
<td> Actors: {{movie_data.actors}}</td>
</tr>
<tr>
<td> Summary: {{movie_data.summary}}</td>
</tr>
<tr>
<td colspan ="2"> {{movie_data.trailer}} </td>
</table>
</div>
</body>
</html>