I need to impl a few basic features in this server:
- display a nice main screen(html) with < select > widget and 2 buttons(one to choose a image from hd to upload and another to send) + TextEditBox for typing a msg
- when the user clicks ‘send’ – take the users choice from the select widget and
- the image will be uploaded
I have managed to implement all of it separately, now when I come to make it all work together I have a problem with app.yaml url paths due to a bad design.
app.yaml looks like:
runtime: python
handlers:
- url: /(.+)
static_files: images/\1
upload: images/(.*)
- url: /.*
script: kserver.py
kserver.py:
class StartPage(webapp.RequestHandler):
def get(self):
select_items = db.GqlQuery( "SELECT * FROM Registration" )
upload_url = blobstore.create_upload_url('/upload')
self.response.out.write(template.render("tst.html", {'select_items': select_items}))
class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
upload_files = self.get_uploads('file')
image = self.request.get("img")
photo = Photo()
photo.imageblob = db.Blob(image)
photo.put()
blob_info = upload_files[0]
self.redirect('/serve/%s' % blob_info.key())
class DownloadImage(webapp.RequestHandler):
def get(self):
photo= db.get(self.request.get("photo_id"))
if photo:
self.response.headers['Content-Type'] = "image/jpeg"
self.response.out.write(photo.imageblob)
else:
self.response.out.write("Image not available")
class Sender(webapp.RequestHandler):
def post(self):
...
...
self.response.headers['Content-Type'] = 'text/html' # reply with 200 OK
self.response.set_status( 200,"OK" )
...
...
class TokenService(webapp.RequestHandler):
def post(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.set_status( 200,"Registration accepted" )
...
application = webapp.WSGIApplication([('/', StartPage),
('/sender',Sender),
('/upload', UploadHandler),
('/i', DownloadImage),
('/serve/([^/]+)?', ServeHandler),
('/token',TokenService)],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()
This is the tst.html :
<html>
<body>
<select name="accountName">
<option value=\"ALL\">ALL</option>
{% for item in select_items %}
<option value="{{ item.accountName }}">{{ item.accountName }}</option>
{% endfor %}
</select>
<form action="sender" method="POST">
<input type="text" id="TextEditBox_1">
<input type="submit" name="submit">
<input type="file" name="img" id="Choose_file">
</form>
</body>
</html>
note, Identation is fine, its only parts of code.
in the project, i have a images directory that contains app.ico and backgroung image (statics)
Okay, now the problem is i get in GAE logs :
Static file referenced by handler not found: images/token
Static file referenced by handler not found: images/sender
this is not intended. it should be in ‘/’ i think
and
"/token 404 48ms 0kb No handlers matched this URL."
in addition, when clicking on “send” it redirects me to a new page with :
The requested URL /sender was not found on this server.
this /token is a POST coming from outside to register to the server – the server than store registrations in db. when “send” button pressed ,it should use this db.register tokens.
any comments on the subject is most welcome. thanx!
The patterns in your app.yaml are too general:
will match just about anything, so the requests that are meant to be handled by code never get there. Try something like this:
which will handle only files that end with gif, png or jpg.