使用谷歌应用引擎和python上传图像

时间:2014-08-25 13:01:02

标签: python html google-app-engine python-2.7

我想上传一张图片,然后查看图片,但它不起作用。日志是:

  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()}} 

任何人都可以解释我如何使用google-app-engine上传图片?谢谢!

1 个答案:

答案 0 :(得分:1)

您的路线中缺少)

'/view/([0-9]+'应为'/view/([0-9]+)