私のプロジェクト(Python、Html、Jinja2を使用)には、映画の写真を含むhome_pageがあり、各写真は映画のデータを含む特定のページにつながります。ページに映画データを表示しようとすると、次のエラーが発生します。
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)
これはhome_pageの私の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_datahtmlコードです:
<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>