0
votes

Im trying to make a form where users can upload a file. And I have a form that takes the users arguments and posts them to the UploadHandler that is mapped to the url that is passed as parameter to the renderfunction. It works up to the point that it renders the form, but when submitting the file it redirects me to a blank page and the console shows me the error:

 ERROR    2013-03-17 11:53:30,769 dev_appserver_blobstore.py:404] Could not find session       for ahhkZXZ-a3Vyc3NhbW1hbmZhdHRuaW5nYXJyGwsSFV9fQmxvYlVwbG9hZFNlc3Npb25fXxgxDA
 INFO     2013-03-17 11:53:30,779 dev_appserver.py:3104] "POST /_ah/upload/ahhkZXZ-   a3Vyc3NhbW1hbmZhdHRuaW5nYXJyGwsSFV9fQmxvYlVwbG9hZFNlc3Npb25fXxgxDA HTTP/1.1" 404 -

Im still new at this but I can't seem to find whats going on. I understand that the mappings somehow f***s up, but still I don't understand why it doesn't redirect me to the correct handler (SummaryHandler)?

The class that serves the form for file upload:

class CreateHandler(BaseHandler):
def get(self):
    self.render('create.html', upload_url = blobstore.create_upload_url('/upload'))

The html-form ('create.html'):

<h2 class="main-title">Upload a file!</h2>  
<form action="{{upload_url}}" method="post" enctype="multipart/form-data">
    <label>
        <div>Upload:</div> 
        <input type="file" name="file" accept="application/pdf"><br> 
    </label>
    <div class="error">
        {{file_error}}
    </div>
    <br>
    <input type="submit" name="submit" value="Submit"> 
 </form>

The uploadhandler that takes the form args:

class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
    upload_files = self.get_uploads('file')  
    blob_info = upload_files[0]

    #validation of form-vars
    have_error=False
    file_data=dict()
    if not (blob_info.content_type == 'application/pdf'):
        file_data['file_error']="You can only upload files in pdf-format."
        have_error=True
    if have_error:
        self.render('create.html', **file_data)
    else:
        #'parent' is just some stuff for organising the database. 
        #Not important in this context
        the_file = A_File(parent = summary_key(),   
                          file_key=str(blob_info.key()))
        the_file.put()
        if the_file:
            self.redirect('/summary/%s' % str(the_file.key().id()))
        else:
            _error="Could not find the file"
            self.render('welcome.html', error=_error)

The application handlers and their mappings:

app = webapp2.WSGIApplication([
(r'/', WelcomeHandler),
(r'/create', CreateHandler),
(r'/upload', UploadHandler),
(r'/summary/(\d+)', SummaryHandler) #I have not included this handler in the text
], debug=True)
1
Did you manage to run the bare-bone complete example? developers.google.com/appengine/docs/python/blobstore/… - Lipis
Yes I did, do you think it has something to do with my problem? - patriques
If the example worked then it has nothing to do with the example.. check my answer. - Lipis

1 Answers

1
votes

It might be the fact that this session expired or it was used. The urls that created using create_upload_url have an expiration (I think 10 min). Before actually posting you might want to refresh that URL using JavaScript.

Also if for any reason the file was uploaded in the Blobstore you won't be able to use the same URL again, it is only good for one request (could have multiple files though).

In your example, try to remove all the file specific checks, try to upload something and check through the administrative console in the Datastore Viewer if you have any blobs or session keys. This can be accessed through this URL:

 http://localhost:8080/_ah/admin/datastore