我的Python代码如下:
DEFAULT_GUESTBOOK_NAME = 'default_guestbook'
def guestbook_key(guestbook_name=DEFAULT_GUESTBOOK_NAME):
return ndb.Key('Guestbook', guestbook_name)
class Author(ndb.Model):
identity = ndb.StringProperty(indexed=False)
email = ndb.StringProperty(indexed=False)
class Greeting(ndb.Model):
author = ndb.StructuredProperty(Author)
content = ndb.StringProperty(indexed=False)
date = ndb.DateTimeProperty(auto_now_add=True)
pdf = ndb.BlobProperty(indexed=False)
class MapPage(webapp2.RequestHandler):
def get(self):
guestbook_name = self.request.get('guestbook_name',
DEFAULT_GUESTBOOK_NAME)
greetings_query = Greeting.query(
ancestor=guestbook_key(guestbook_name)).order(-Greeting.date)
greetings = greetings_query.fetch(10)
user = users.get_current_user()
template_values = {
'user': user,
'user_mail': user.email(),
'greetings': greetings,
'guestbook_name': urllib.quote_plus(guestbook_name),
'def_name': DEFAULT_GUESTBOOK_NAME
}
path = os.path.join(os.path.dirname(__file__), 'mappage.html')
self.response.out.write(template.render(path, template_values))
class Guestbook(webapp2.RequestHandler):
def post(self):
guestbook_name = self.request.get('guestbook_name',
DEFAULT_GUESTBOOK_NAME)
greeting = Greeting(parent=guestbook_key(guestbook_name))
if users.get_current_user():
greeting.author = Author(
identity=users.get_current_user().user_id(),
email=users.get_current_user().email())
greeting.content = self.request.get('content')
greeting.pdf = self.request.get('pdf')
greeting.put()
query_params = {'guestbook_name': guestbook_name}
self.redirect('/mappage?' + urllib.urlencode(query_params))
基本上发生的是,班级留言簿存储带有内容和pdf的问候语。
我的HTML端有这样的代码来上传pdf文件:
<form class="notdef" action="/sign?guestbook_name={{ guestbook_name }}" method="post" style="display:none" enctype="multipart/form-data">
<div><textarea name="content" rows="3" cols="60"></textarea></div>
<br>
<div><input type="file" name="pdf">
<hr class="notdef" style="display:none">
<div><input type="submit" value="Save"></div>
</form>
和要显示的代码:
<blockquote>{{ greeting.content }}</blockquote>
<embed src="{{ greeting.pdf}}" width="500" height="375" type="application/pdf">
然而,我收到错误
DjangoUnicodeDecodeError: 'utf8' codec can't decode byte 0xe2 in position 11: invalid continuation byte. You passed in ..... (long string)
如何解决这个问题才能看到pdf?其他帖子说{{greeting.pdf}}是字符串格式,但如何更改它以便显示PDF?
答案 0 :(得分:0)
您应该将embed标记的src
属性设置为pdf的路径,而不是尝试插入实际的pdf数据。