jeudi 30 avril 2015

Render image with jinja2 and ndb BlobProperty

What is the simplest way to upload image and render it on another page using jinja2 and ndb. My app.py includes:

class Student(ndb.Model):
    name = ndb.StringProperty()
    avatar = ndb.BlobProperty()

JINJA_ENVIRONMENT = jinja2.Environment(
    loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
    extensions=['jinja2.ext.autoescape'])  

My form in html file is:

<form method='post' action='/Student' enctype ='multipart/form-data'>
First Name <input type='text' name='firstName'/> </br>
Avatar <input type ='file' name ='avatar' /><br>
<input type='submit' value='Save'>
</form>

My post method for the form is:

def post(self):
    firstName = self.request.get('firstName')   
    student = Student()
    student.firstName = firstName
    avatar = self.request.get('avatar')
    avatar = images.resize(avatar, 32, 32)
    student.avatar = avatar 
    student.put()

My get method to get the information of the student is:

def get(self):            
    template = JINJA_ENVIRONMENT.get_template('student.html')
    students = Student.query().fetch()

    template_values = {'students': students}
    self.response.write(template.render(template_values))

In my html file I use:

{% for student in students %}
    {{ student.firstName }} - {{ student.avatar }} <br/>
{% endfor %}

There is no problem when I submit the form but I when I go to the html file which display the inf, it shows the error in the terminal: UnicodeDecodeError: 'ascii' codec can't decode byte 0x89 in position 0: ordinal not in range(128)

Please help!!

Aucun commentaire:

Enregistrer un commentaire