UnicodeDecodeError. While rendering the blob image

自作多情 提交于 2019-12-12 00:32:43

问题


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?


    回答1:


    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.



    来源:https://stackoverflow.com/questions/11475717/unicodedecodeerror-while-rendering-the-blob-image

    易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
    该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!