UnicodeDecodeError错误。呈现blob图像时 [英] UnicodeDecodeError. While rendering the blob image

查看:155
本文介绍了UnicodeDecodeError错误。呈现blob图像时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试添加上传图片功能。
这个类保存了新的实体:

$ p $ class NewPost(Handler):
def render_newpost(self,title ='',content ='',error =''):
self.render(newpost.html,title = title,content = content,error = error)


def get(self):
user_cookie = self.request.cookies.get('user')
if user_cookie:
user_id = user_cookie.split('|')[0 ]
如果hash_str(user_id)== user_cookie.split('|')[1]:
user = Users.get_by_id(int(user_id))
self.render_newpost()
else:
self.redirect('/')


def post(self):
title = self.request.get(title)
content = self.request.get(content)
image = self.request.get(file)

如果标题和内容:
p =文章(title = title,content = content)
p.image = db.Blob(image)
p.put()
self.redirect(/)
else:
error =输入标题和文本
self.render_newpost(title,content,错误)

这是我的头版渲染类:

  class Main(Handler):
def get(self):
posts = db.GqlQuery(SELECT * FROM Posts Order by DESC LIMIT 10 )
user_cookie = self.request.cookies.get('user')
if user_cookie:
user_id = user_cookie.split('|')[0]
if hash_str (user_id)== user_cookie.split('|')[1]:
user = Users.get_by_id(int(user_id))
self.render(front.html,posts = posts, user = user)
else:
self.response.headers ['Content-Type'] =image / jpeg
self.render(front.html,posts = posts)

表单输入数据:

 < form method =postenctype =multipart / form-data> 
< div class =newpost>
< label>图片:< / label>
< input type =filename =file/>
< div class =label>标题:
< input type =textname =titlevalue ={{title}}/>
< / div>
< hr>
< div class =label>内容:
< textarea name =content> {{content}}< / textarea>
< / div>
< / div>
< input type =submit/>
< div class =error> {{error}}< / div>
< / form>

以下是我的主页模板:(问题出现在这里!)

  {%发布在帖子中%} 
{%如果post.image%}
< ; li>< img src =/ static / images / {{post.image}}/>
{%endif%}
< h4>< a href =#> {{post.title}}< / a>< / h4>
< p class =zoom> {{post.content}}< / p>
{%endfor%}

应用程序成功存储图像,但是当它返回到前端尝试呈现图片时出现此错误:

文件/home/wanhrust/google_appengine/newsite/templates/front.html,第54行顶级模板代码

  • {{post.image}}
    UnicodeDecodeError:'ascii'编解码器无法在位置0解码字节0xff:序号不在范围内(128)


    我一直在Google上搜寻几个小时但没有结果。任何帮助?

    解决方案

    问题是,您正尝试在html文档中嵌入图像,这是不可能的。 Post.image存储表示图像的字节。



    如果你想在你的html中包含图像,你需要添加像这样的内容

      {%if post.image_id%} 
    < li>< img src =/ image / {{post.image_id}} />
    {%endif%}



    其中/ image /是一个处理程序,它返回(设置apprpriate内容类型)。



    我也建议您使用 Blobstore API


    I am trying to add uploading images feature. This class saves new entity:

    class NewPost(Handler):
        def render_newpost(self, title='' , content='', error = ''):
            self.render("newpost.html", title = title, content = content, error = error)
    
    
        def get(self):
            user_cookie = self.request.cookies.get('user')
            if user_cookie:
                user_id = user_cookie.split('|')[0]
                if hash_str(user_id) == user_cookie.split('|')[1]:
                    user = Users.get_by_id(int(user_id))
                    self.render_newpost()
            else:
                self.redirect('/')
    
    
        def post(self):
            title = self.request.get("title")
            content = self.request.get("content")
            image = self.request.get("file")
    
            if title and content:
                p = Posts(title = title, content = content)
                p.image=db.Blob(image)
                p.put()
                self.redirect("/")
            else:
                error = "Enter both title and text"
                self.render_newpost(title, content, error)
    

    Here is my front page render class:

    class Main(Handler):
        def get(self):
            posts = db.GqlQuery("SELECT * FROM Posts Order by created DESC LIMIT 10")
            user_cookie = self.request.cookies.get('user')
            if user_cookie:
                user_id = user_cookie.split('|')[0]
                if hash_str(user_id) == user_cookie.split('|')[1]:
                    user = Users.get_by_id(int(user_id))
                    self.render("front.html", posts = posts, user=user)
            else:
                self.response.headers['Content-Type'] = "image/jpeg"
                self.render("front.html", posts = posts)
    

    form to enter data:

         <form method="post" enctype="multipart/form-data">
            <div class="newpost">
                <label>Image:</label>
                <input type="file" name="file"/>
                <div class="label">Title: 
                    <input type="text" name="title" value="{{title}}"/>
                </div>
               <hr> 
                <div class="label">Content:
                    <textarea name="content">{{content}}</textarea>
                </div>
            </div>
            <input type="submit"/>
            <div class="error">{{error}}</div>
        </form>
    

    and here is my home page template: (The problem appears here!)

                {% for post in posts %}
                    {% if post.image %}
                    <li><img src="/static/images/{{post.image}}"/>
                    {% endif %}
                        <h4><a href="#">{{post.title}}</a></h4>
                        <p class="zoom">{{post.content}}</p>
                {% endfor %}
    

    App successfully stores image, but when it returns to the front page trying to render image it gives this error:

    File "/home/wanhrust/google_appengine/newsite/templates/front.html", line 54, in top-level template code

  • {{post.image}} UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128)

    I've been googling for several hours but without results. Any help?

    解决方案

    The problem is that you are trying to embed an image in an html document which is not possible. Post.image is storing the bytes representing the image.

    If you want to include the image in your html, you need to add something like this

    {% if post.image_id %}
        <li><img src="/image/{{post.image_id}}" />
    {% endif %}
    

    Where /image/ is a handler that returns the content of the image (setting the apprpriate content-type).

    I also would recommend you to use the Blobstore API.

    这篇关于UnicodeDecodeError错误。呈现blob图像时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆