I want the user to be able to upload images to Google App Engine. I have the following (Python):
class ImageData(ndb.Model):
name = ndb.StringProperty(indexed=False)
image = ndb.BlobProperty()
Information is submitted by the user using a form (HTML):
<form name = "input" action = "/register" method = "post">
name: <input type = "text" name = "name">
image: <input type = "file" name = "image">
</form>
Which is then processed by:
class AddProduct(webapp2.RequestHandler):
def post(self):
imagedata = ImageData(parent=image_key(image_name))
imagedata.name = self.request.get('name')
imagedata.image = self.request.get('image')
imagedata.put()
However, when I try to upload an image, lets say "Book.png", I get the error:
BadValueError: Expected str, got u'Book.png'
Any idea what is going on? I have been working with GAE for quite some time, but this is the first time I had to use blobs.
I used this link: https://developers.google.com/appengine/docs/python/images/usingimages
which uses db, not ndb.
I also tried storing the image in a variable first like in the link:
storedInfo = self.request.get('image')
and then storing it:
imagedata.image = ndb.Blob(storedInfo)
Which ALSO gives me an error:
AttributeError: 'module' object has no attribute 'Blob'
Thanks in advance.
Had the same prob.
just replace
imagedata.image = self.request.get('image')
with:
imagedata.image = str(self.request.get('image'))
also your form needs to have enctype="multipart/form-data
<form name = "input" action = "/register" method = "post" enctype="multipart/form-data">
There is a great example in the documentation that describes how to upload files to the Blobstore using a HTML form: https://developers.google.com/appengine/docs/python/blobstore/#Python_Uploading_a_blob
The form should point to a url generated by blobstore.create_upload_url('/foo') and there should be a subclass of the BlobstoreUploadHandler at /foo like this:
class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
upload_files = self.get_uploads('file')
blob_info = upload_files[0]
imagedata = ImageData(parent=image_key(image_name))
imagedata.name = self.request.get('name')
imagedata.image = blob_info.key()
imagedata.put()
For this to work, you should change your data model such that in ImageData, image referes to a ndb.BlobKeyProperty().
You can serve your image simply from a url generated by images.get_serving_url(imagedata.image), optionally resized and cropped.
You must add enctype="multipart/form-data" to your form in order for this to work
<form name = "input" action = "/register" method = "post" enctype="multipart/form-data">
name: <input type = "text" name = "name">
image: <input type = "file" name = "image">
</form>
Related
I have a HTML form
<form>
<input type="text" name="keyword" id="keyword" />
<input type="submit">Submit</input>
on submits I want to pass form data to flask app
requests.py
def search_news(keyword):
search_news_url = 'https://newsapi.org/v2/everything?q={}&language=en&apiKey={}'.format(keyword,api_key)
with urllib.request.urlopen(search_news_url) as url:
search_news_data = url.read()
search_news_response = json.loads(search_news_data)
search_news_results = None
if search_news_response['articles']:
search_news_list = search_news_response['articles']
search_news_results = process_search_results(search_news_list)
return search_news_results
def process_search_results(search_news_list):
news_results = []
for search_results_item in search_news_list:
author = search_results_item.get('author')
title = search_results_item.get('title')
description = search_results_item.get('description')
url = search_results_item.get('url')
urlToImage = search_results_item.get('urlToImage')
publishedAt = search_results_item.get('publishedAt')
content = search_results_item.get('content')
totalResults = search_results_item.get('totalResults')
if content:
news_results_object = Everything(author,title,description,url,urlToImage,publishedAt,content,totalResults)
news_results.append(news_results_object)
return news_results
views.py
from ..requests import get_everything,search_news
....
#main.route('/')
def index():
everything = get_everything()
title = 'News Highlight'
searching_news = request.args.get('keyword')
if searching_news:
redirect(url_for('.search',keyword = searching_news))
return render_template('index.html',title = title,everything = everything)
....
#main.route('/search/<keyword>')
def search(keyword):
keyword_list = keyword.split(" ")
keyword_format = '%20'.join(keyword_list)
searched_news = search_news(keyword_format)
title = f'Search results for {keyword} '
return render_template('search.html',searched_news = searched_news)
on form submits it appends form data to url like this:
http://127.0.0.1:5000/?keyword=game+of+thrones
I've tried using post methods but i get a server does not support method error. Can anyone help please.
but when I append link like this:
http://127.0.0.1:5000/search/game%20%of%thrones
the app searches and displays results
By default, form data is submitted via the URL's query string if you don't tell it to behave differently (the method defaults to GET).
Post
If you want to send the data to flask using a POST request, this is the way to go:
Make sure to tell your HTML form to use method="POST" and tell it where to send it's data via the action attribute:
<form method="post" action="/search">
<input type="text" name="keyword" id="keyword" />
<input type="submit">Submit</input>
</form>
Tell your server-side view to accept POST requests and fetch the sent data from request.form. In views.py use
#main.route('/search/' methods=['POST'])
def search():
keyword = request.form['keyword']
...
Get
If you want to use a GET request and pass the data via query string set your form's method to get
<form method="get" action="/search">
<input type="text" name="keyword" id="keyword" />
<input type="submit">Submit</input>
</form>
On submit, your browser will append the values entered in the input field to the URL as a query string: ?keyword=<whatever-you-entered>.
Tell your server-side view to accept GET requests and fetch query string data via request.args.get(). In views.py use
#main.route('/search/' methods=['GET'])
def search():
keyword = request.args.get('keyword')
...
MDN has got a nice article with more details around sending and retreiving form data with HTML and Python, this might be worth a read, too.
I know how to upload multiple files through django, but I have a problem when uploading a folder if there are subfolders in it. The django can't receive subfolders. I found the reason, because browser use '.' to represent a folder, but django can't parse it then stop parsing. Is there an elegant way to fix it?
python code:
def uploader_single(request):
data = {}
if request.method == 'POST':
if True:
for afile in request.FILES.getlist('file'):
new_file = UploadFileSingle(file = afile)
new_file.save()
return HttpResponseRedirect('')
else:
print "form is not valid"
return HttpResponseRedirect('')
else:
print 'not post'
Python code:
class UploadFileSingle(models.Model):
file = models.FileField(upload_to='files/%Y/%m/%d', models.FilePath)
uploaded_at = models.DateTimeField(auto_now_add=True)
models.FilePathField.recursive = True
models.FilePathField.allow_folders = True
updated_at = models.DateTimeField(auto_now=True)
def some_folder = FilePathField(path='some_path', recursive=True, allow_files=True, allow_folders=True,)'
HTML code:
<input type="file" name="file" multiple = "true" webkitdirectory="true" directory = "true"/>
There is newer topic which asks the same question and I have tried to answer:
Django directory upload get sub-directory names
Basically it is the default behavior of Django if you want to have different behavior you need to write your own upload handlers
I came up with easy solution for this problem.
You could get folder name via html and javascript in frontend
pass it as a value to hidden form field
in backend you can create a directory with that name
and upload files in this directory.
HTML
<input type="hidden" name="dir_name" id="id_dir_name">
<input type="file" name="file" onchange="selectFolder(event)" webkitdirectory="" multiple="" required="" directory="" id="id_file">
JS
function selectFolder(e) {
var theFiles = e.target.files;
var relativePath = theFiles[0].webkitRelativePath;
var folder = relativePath.split("/");
$("#" + id).val(folder[0]);
}
views
directory_name = form.cleaned_data['dir_name']
os.mkdir(os.path.join(settings.MEDIA_ROOT, directory_name))
handle_uploaded_file(request.FILES['file'], directory_name)
you can use django filer
rerfer:https://github.com/stefanfoulis/django-filer
I am am using Google App Engine in Python and I want users to be able to upload a video, which is functioning properly by following their basic example but then I want to be able to get the user to add additional information about the video, like the title and category and a summary while it is uploading. Is there any way I can make the upload asynchronous so that the user doesn't have to wait the whole time the video is uploading?
I know about the create_upload_url_async() method but that doesn't do what I am trying.
Right now I have the following which uploads and serves but I want to make an intermediate step where the user can add the info preferably on the same screen as the UploadHandler while it is uploading.
class VideoHandler(BlogHandler):
def get(self):
user = self.get_user()
upload_url = blobstore.create_upload_url('/uploadingvideo')
self.render('videohandler.html', user=user, upload_url=upload_url)
class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
upload_files = self.get_uploads('file') # 'file' is file upload field in the form
blob_info = upload_files[0]
self.redirect('/serve/%s' % blob_info.key())
class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler):
def get(self, blob_key):
blob_key = str(urllib.unquote(blob_key))
if not blobstore.get(blob_key):
self.error(404)
else:
self.send_blob(blobstore.BlobInfo.get(blob_key))
I'd be glad to provide more information if you need it.
this has been troubling me alot too clifgray. the solution is to build your form around the blob form.
Here's google's example on how to submit data to the blob store.
class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
upload_files = self.get_uploads('file') # 'file' is file upload field in the form
blob_info = upload_files[0]
self.redirect('/serve/%s' % blob_info.key())
Just remove the self redirect bit at the bottom of the code and add some code to handle your form.
class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
upload_files = self.get_uploads('file') # 'file' is file upload field in the form
blob_info = upload_files[0]
greeting = Greeting()
if users.get_current_user():
greeting.author = users.get_current_user()
greeting.video = str(blob_info.key())
greeting.content = self.request.get('content')
greeting.put()
Your form in your django template should look something like this:
form action="{{ upload_url }}" enctype="multipart/form-data" method="post">
textarea name="content" placeholder="write something about this video.." tabindex="1" rows="2" cols="40"></textarea>
input name="file" type="file" >
input name="submit" type="submit" value="Submit" />
/form>
There you have it. hope this helps
I have been banging my head against the wall on this one, for some reason I am having trouble tying the different aspects of Google App Engine together to make this work.
Basically I want to let a user upload a photo to the Blobstore, which I have working in the below code, and then I want to put the BlobKey into a list which will be stored in a database entity. So here is my code to upload the image and where in here can I get the BlobKey so that I can store it?
class MainHandler(BlogHandler):
def get(self):
upload_url = blobstore.create_upload_url('/upload')
self.response.out.write('<html><body>')
self.response.out.write('<form action="%s" method="POST" enctype="multipart/form-data">' % upload_url)
self.response.out.write("""Upload File: <input type="file" name="file"><br> <input type="submit" name="submit" value="Submit"> </form></body></html>""")
#there is a lot more code in here where I get all the following info but it isn't relevant
location_db = Location(
description=description,
submitter=submitter,
user_id=user_id,
title=title,
locationtype = locationtype)
#This is what I would like to do but I don't know where to get thr BlobKey
location_db.blobRefs.append(BlobKey)
location_db.put()
for b in blobstore.BlobInfo.all():
self.response.out.write('<li>' + str(b.filename) + '')
class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
upload_files = self.get_uploads('file')
blob_info = upload_files[0]
self.redirect('/main')
class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler):
def get(self, blob_key):
blob_key = str(urllib.unquote(blob_key))
if not blobstore.get(blob_key):
self.error(404)
else:
self.send_blob(blobstore.BlobInfo.get(blob_key), save_as=True)
Here:
blob_info = upload_files[0]
self.redirect('/serve/%s' % blob_info.key())
I think it's that
blob_info.key()
you are missing. Grab that, stuff it into your list. Docs also note:
In this handler, you can store the blob key with the rest of your application's data model. The blob key itself remains accessible from the blob info entity in the datastore.
https://developers.google.com/appengine/docs/python/blobstore/overview#Serving_a_Blob
I'm trying to associate a video file to a record with a bunch of properties, but can't seem to allow the user to do everything in one form - name the video, provide description and answer some question, AND upload the file.
Here are the steps I'd like to perform:
User is served with a page containing a form with the following fields: Name, Description, File selector.
The file gets stored as a blob and the id gets recorded together with name and description.
Does anyone have any examples of doing this I could learn from or a tutorial you could point me to? The one from google only shows uploading the file and getting redirected to it.
Thanks and sorry for a newbish question!
http://demofileuploadgae.appspot.com/ - My demo uploader to the blobstore.
My code for the upload: http://code.google.com/p/gwt-examples/source/browse/trunk/DemoUpload/src/org/gonevertical/upload/#upload/server%3Fstate%3Dclosed
Here's the code I'm using to upload images and associate them with articles. The toughest bit was getting the article id to get to the upload handler, I solved it by setting the file name as the article id to get around the problem.
from lib import urllib2_file
from lib.urllib2_file import UploadFile
# this view serves a task in a queue
def article(request):
article = Article.objects.get(id=form.cleaned_data['article'])
try:
image = StringIO(urllib2.urlopen(image_url).read())
except (urllib2.HTTPError, DownloadError):
article.parsed = True
article.save()
else:
image = UploadFile(image, '.'.join([str(article.id), image_url.rsplit('.', 1)[1][:4]]))
upload_url = blobstore.create_upload_url(reverse('Articles.views.upload'))
try:
urllib2.urlopen(upload_url, {'file': image})
except (DownloadError, RequestTooLargeError):
pass
return HttpResponse(json.dumps({'status': 'OK'}))
def upload(request):
if request.method == 'POST':
blobs = get_uploads(request, field_name='file', populate_post=True)
article = Article.objects.get(id=int(blobs[0].filename.split('.')[0]))
article.media = blobs[0].filename
article.parsed = True
article.save()
return HttpResponseRedirect(reverse('Articles.views.upload'))
else:
return HttpResponse('meow')
def upload(request):
if request.method == 'POST':
blobs = get_uploads(request, field_name='file', populate_post=True)
article = Article.objects.get(id=int(blobs[0].filename.split('.')[0]))
article.media = blobs[0].filename
article.parsed = True
article.save()
return HttpResponseRedirect(reverse('Articles.views.upload'))
else:
return HttpResponse('meow')
# this serves the image
def image(request):
blob = BlobInfo.gql("WHERE filename='%s' LIMIT 1" % request.form.cleaned_data['id'])[0]
return HttpResponse(BlobReader(blob.key()).read(),
content_type=blob.content_type)
Also you'll need this http://fabien.seisen.org/python/urllib2_file/
Here is how I did it. It is more straight forward than you think. Note the following taken from Blobstore overview.
"When the Blobstore rewrites the user's request, the MIME parts of the uploaded files have their bodies emptied, and the blob key is added as a MIME part header. All other form fields and parts are preserved and passed to the upload handler." In the upload handler is where you can do whatever it is you want with the other form fields.
class Topic(db.Model):
title = db.StringProperty(multiline=False)
blob = blobstore.BlobReferenceProperty()
imageurl = db.LinkProperty()
class MainHandler(webapp.RequestHandler):
def get(self):
upload_url = blobstore.create_upload_url('/upload')
self.response.out.write('<html><body>')
self.response.out.write('<form action="%s" method="POST" enctype="multipart/form-data">' % upload_url)
self.response.out.write("""Upload File: <input type="file" name="file"><br>
<div><label>Title:</label></div>
<div><textarea name="title" rows="1" cols="25"></textarea></div><input type="submit"
name="submit" value="Submit"> </form>""")
self.response.out.write('<br><br><h2>TOPIC LIST</h2><table border="1"><tr><td>')
for topic in Topic.all():
self.response.out.write('<div><img src="%s=s48"/>' % topic.imageurl)
self.response.out.write('<div><b>Image URL: </b><i>%s</i></div>' % topic.imageurl)
self.response.out.write('<div><b>Title: </b><i>%s</i></div>' % topic.title)
self.response.out.write('</td></tr></table><br>')
self.response.out.write('</body></html>')
class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
upload_files = self.get_uploads('file') # 'file' is file upload field in the form
blob_info = upload_files[0]
topic = Topic()
topic.title = self.request.get("title")
topic.blob = blob_info.key()
topic.imageurl = images.get_serving_url(str(blob_info.key()))
topic.put()
self.redirect('/')
def main():
application = webapp.WSGIApplication(
[('/', MainHandler),
('/upload', UploadHandler),
], debug=True)
run_wsgi_app(application)