calin
calin

Reputation: 13

upload images using google app engine and python

I am tring to upload an image and then to view the image but it doesn't work. the logs are:

  File "C:\Python27\lib\re.py", line 242, in _compile

    raise error, v # invalid expression

error: unbalanced parenthesis


main.py

import webapp2
import os
import jinja2

from google.appengine.ext import db
from google.appengine.api import images

template_dir = os.path.join(os.path.dirname(__file__), 'templates')
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir),
                               autoescape = True)

def render_str(template, **params):
    t = jinja_env.get_template(template)
    return t.render(params)

class MainHandler(webapp2.RequestHandler):
    def render(self, template, **kw):
        self.response.out.write(render_str(template, **kw))

    def write(self, *a, **kw):
        self.response.out.write(*a, **kw)

def img_key(name = 'default'):
    return db.Key.from_path('imgs', name)


class BazaDate(db.Model):
    avatar = db.BlobProperty()

class Image_View(MainHandler):
    def get(self, img_id):
        key = db.Key.from_path('BazaDate', img_id, parent=img_key())
        image = db.get(key)

        if not image:
            self.error(404)
            return

        self.render("view.html", image = image)

class Upload(MainHandler):
    def get(self):       
        self.render('upload.html')
    def post(self):
        avatar = self.request.get('img')
        p=BazaDate(avatar=avatar)
        p.put()
        self.redirect('/view/%s' % str(p.key().id()))
app = webapp2.WSGIApplication([('/', Upload),
                                ('/view/([0-9]+)', Image_View)],
                              debug=True)


upload.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <form enctype="multipart/form-data" method="post">
    <input type="file" name="img">
    <input type="submit">
</form>
</body>
</html>


view.html

{{image.render()}} 

Can anyone explain me how can i upload images using using google-app-engine? Thank you!

Upvotes: 0

Views: 91

Answers (1)

bigblind
bigblind

Reputation: 12867

You are missing a ) in your route:

'/view/([0-9]+' should be '/view/([0-9]+).

Upvotes: 1

Related Questions