I have a website and I want to allow admins to upload pictures through an upload form.
HTML: On a page blob.html
:
<form name="uploader" action="/blob/" method="post" enctype="multipart/form-data">
<input type="file"><br>
<input type="submit" value="Upload">
</form>
blob.py:
import os
import webapp2
import jinja2
# blobstore api
from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers
template_env = jinja2.Environment(loader=jinja2.FileSystemLoader(os.getcwd()))
class MainPage(webapp2.RequestHandler):
def get(self):
upload_url = blobstore.create_upload_url('/blob/')
template = template_env.get_template('blob.html')
context = {
'upload_url': upload_url
}
self.response.write(template.render(context))
def post(self):
upload_files = self.get_uploads('uploader')
blob_info = upload_files[0]
# So some logging here and concept to store the reference of the blob key.
#self.redirect('/')
app.yaml:
...stuff...
handlers:
- url: /blob/
script: blob.app
login: admin
...stuff...
But when I go to the /blob/
on my website, I get this error:
Traceback (most recent call last):
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 239, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 301, in _LoadHandler
raise err
ImportError: <module 'blob' from '/base/data/home/apps/s~themoringathebetter/1.372284039078482061/blob.pyc'> has no attribute app
Full rewrite
So practically you want someone to code a small boilerplate for you, here you are. Use
/uploadform/
for the form and/
for the index. I kept is as simple as I could.BoilerPlate Form for image uploads
Make sure you have PIL installed. (Tip: For osx use easy_install pil)