Google App Engine: 404 Resource not found - python

I am trying to build a basic blog model using Google App Engine in Python. However, something's wrong with my code I suppose, and I am getting a 404 error when I try to display all the posted blog entries on a single page. Here's the python code:
import os
import re
import webapp2
import jinja2
from string import letters
from google.appengine.ext import db
template_dir = os.path.join(os.path.dirname(__file__), 'templates')
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir), autoescape=True)
class Handler(webapp2.RequestHandler):
def write(self, *a, **kw):
self.response.out.write(*a, **kw)
def render_str(self, template, **params):
t = jinja_env.get_template(template)
return t.render(params)
def render(self, template, **kw):
self.write(self.render_str(template, **kw))
def post_key(name = "dad"):
return db.Key.from_path('blog', name)
class Blogger(db.Model):
name = db.StringProperty()
content = db.TextProperty()
created = db.DateTimeProperty(auto_now_add = True)
def render(self):
self._render_text = self.content.replace('\n', '<br>')
return render_str("post.html", p = self)
class MainPage(Handler):
def get(self):
self.response.write("Visit our blog")
class BlogHandler(Handler):
def get(self):
posts = db.GqlQuery("SELECT * FROM Blogger order by created desc")
self.render("frontblog.html", posts = posts)
class SubmitHandler(Handler):
def get(self):
self.render("temp.html")
def post(self):
name = self.request.get("name")
content = self.request.get("content")
if name and content:
a = Blogger(name = name, content = content, parent = post_key())
a.put()
self.redirect('/blog/%s' % str(a.key().id()))
else:
error = "Fill in both the columns!"
self.render("temp.html", name = name, content = content, error = error)
class DisplayPost(Handler):
def get(self, post_id):
po = Blogger.get_by_id(int(post_id))
if po:
self.render("perma.html", po = po)
else:
self.response.write("404 Error")
app = webapp2.WSGIApplication([('/', MainPage),
('/blog', BlogHandler),
('/blog/submit', SubmitHandler),
('/blog/([0-9]+)', DisplayPost)], debug=True)
After posting my content, it gets redirected to a permalink. However, this is the error I am getting on submitting my post:
404 Not Found
The resource could not be found
Here's the frontblog.html source code, in case that would help:
<!DOCTYPE html>
<html>
<head>
<title>CS 253 Blog</title>
</head>
<body>
<a href="/blog">
CS 253 Blog
</a>
<div id="content">
{% block content %}
{%for post in posts%}
{{post.render() | safe}}
<br></br>
{%endfor%}
{% endblock %}
</div>
</body>
</html>
So basically, I am not being redirected to the permalink page. What seems to be the problem?

When you create your post, you're giving it a parent (not sure why). But when you get it, you do so by the ID only, and don't take into account the parent ID. In the datastore, a key is actually a path consisting of all the parent kinds and IDs/names and then those of the current entity, and to get an object you need to pass the full path.
Possible solutions here:
Drop the parent key, since it isn't doing anything here as you're always setting it to the same value;
Use it when you get the object: Blogger.get_by_id(post_id, parent=parent_key()) - obviously this only works if the parent is always the same;
Use the full stringified key in the path, rather than just the ID, and do Blogger.get(key) - you'll also need to change the route regex to accept alphanumeric chars, eg '/blog/(\w+)', and change the redirect to '/blog/%s' % a.key().

Related

How to make a 'permalink' detail page displaying two images in Google App Engine webapp2 guestbook tutorial

I'm following the Google App Engine tutorial (here's their complete demo code on GitHub) and would like to:
Allow a guestbook user to upload an extra image in addition to 'avatar' when posting a greeting. This could be called 'other'
After posting a greeting, redirect the user to a greeting detail page with a URL like /greeting/numeric-id instead of the main page listing all greetings.
Display the detail page with images using a Jinja2 template called detail.html
I'm having trouble understanding:
A) How to write the redirect code that is called once a greeting is published so it redirects to a URL like /greeting/numeric-id.
B) How to write the Detail view and template page that the user is redirected to so the greeting id and images are displayed.
Here's a diagram showing what I want to do:
Here's guestbook.py:
import os
import urllib
from google.appengine.api import images
from google.appengine.api import users
from google.appengine.ext import ndb
from google.appengine.ext.webapp import blobstore_handlers
from google.appengine.ext import blobstore
import jinja2
import webapp2
JINJA_ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=['jinja2.ext.autoescape'],
autoescape=True)
DEFAULT_GUESTBOOK_NAME = 'default_guestbook'
def guestbook_key(guestbook_name=None):
"""Constructs a Datastore key for a Guestbook entity with name."""
return ndb.Key('Guestbook', guestbook_name or 'default_guestbook')
class Author(ndb.Model):
"""Sub model for representing an author."""
identity = ndb.StringProperty(indexed=False)
email = ndb.StringProperty(indexed=False)
class Greeting(ndb.Model):
"""A model for representing an individual Greeting entry."""
author = ndb.StructuredProperty(Author)
date = ndb.DateTimeProperty(auto_now_add=True)
avatar = ndb.BlobProperty(indexed=False, required=True)
other = ndb.BlobProperty(indexed=False, required=True)
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.out.write('<html><body>')
guestbook_name = self.request.get('guestbook_name')
greetings = Greeting.query(
ancestor=guestbook_key(guestbook_name)) \
.order(-Greeting.date) \
.fetch(10)
self.response.out.write("""
<form action="/sign?%s"
enctype="multipart/form-data"
method="post">
<label>Avatar:</label>
<input type="file" name="avatar"/><br>
<label>Other Image:</label>
<input type="file" name="other"/><br>
<input type="submit" value="Submit">
</form>
</body>
</html>""" % (urllib.urlencode({'guestbook_name': guestbook_name})))
class Image(webapp2.RequestHandler):
""" Handle image stored as blobs of bytes.
No idea how the template knows to select a particular one. """
def get(self):
avatar_greeting_key = ndb.Key(urlsafe=self.request.get('avatar_id'))
other_greeting_key = ndb.Key(urlsafe=self.request.get('other_id'))
avatar_greeting = avatar_greeting_key.get()
other_greeting = other_greeting_key.get()
if avatar_greeting.avatar:
self.response.headers['Content-Type'] = 'image/png'
self.response.out.write(avatar_greeting.avatar)
elif other_greeting.other:
self.response.headers['Content-Type'] = 'image/png'
self.response.out.write(other_greeting.other)
else:
self.response.out.write('No image')
class Guestbook(webapp2.RequestHandler):
def post(self):
guestbook_name = self.request.get('guestbook_name',
DEFAULT_GUESTBOOK_NAME)
greeting = Greeting(parent=guestbook_key(guestbook_name))
if users.get_current_user():
greeting.author = Author(
identity=users.get_current_user().user_id(),
email=users.get_current_user().email())
avatar = self.request.get('avatar')
avatar = images.resize(avatar, 100, 100)
other = self.request.get('other')
other = images.resize(other, 400, 300)
greeting.avatar = avatar
greeting.other = other
greeting.put()
query_params = {'guestbook_name': guestbook_name}
self.redirect('/greeting/%d' % greeting.key.id())
class Detail(webapp2.RequestHandler):
""" Individual greeting. """
def get(self, *args, **kwargs):
guestbook_name = self.request.get('guestbook_name', DEFAULT_GUESTBOOK_NAME)
greeting = Greeting.get_by_id(args[0],
parent=guestbook_key(guestbook_name))
template_values = {
'greeting': greeting,
}
template = JINJA_ENVIRONMENT.get_template('detail.html')
self.response.write(template.render(template_values))
app = webapp2.WSGIApplication([
('/', MainPage),
('/img', Image),
('/sign', Guestbook),
('/greeting/(\d+)', Detail),
], debug=True)
My detail.html template:
<!DOCTYPE html>
{% autoescape true %}
<html>
<head>
<title>Greeting {{ greeting.id }}</title>
</head>
<body>
<h2>Greeting {{ greeting.id }}</h2>
Avatar: <img src="/img?avatar_id={{ greeting.key.urlsafe() }}">
<br>
Other: <img src="/img?other_id={{ greeting.key.urlsafe() }}">
</body>
</html>
{% endautoescape %}
My app.yaml in case it is useful:
runtime: python27
api_version: 1
threadsafe: true
# Handlers match in order, put above the default handler.
handlers:
- url: /stylesheets
static_dir: stylesheets
- url: /.*
script: guestbook.app
libraries:
- name: webapp2
version: latest
- name: jinja2
version: latest
The Error:
Traceback (most recent call last):
File "/Users/simon/Projects/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
File "/Users/simon/Projects/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "/Users/simon/Projects/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/Users/simon/Projects/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/Users/simon/Projects/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/Users/simon/Projects/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/Users/simon/Projects/guestbook/guestbook.py", line 111, in get
self.response.write(template.render(template_values))
File "/Users/simon/Projects/google-cloud-sdk/platform/google_appengine/lib/jinja2-2.6/jinja2/environment.py", line 894, in render
return self.environment.handle_exception(exc_info, True)
File "/Users/simon/Projects/guestbook/detail.html", line 9, in top-level template code
Avatar: <img src="/img?avatar_id={{ greeting.key.urlsafe() }}">
UndefinedError: 'None' has no attribute 'key'
Any help, or even better, example code, would be much appreciated.
A GAE/webapp2 blog tutorial with example code of detail and list views and templates would be great, but perhaps the data structure of the GAE BlobStore is not ideal for a blog?
Update:
If I add the python check code contributed in Dan's answer I get a 500 error instead of the stack trace, and if I try the template checks I get a blank greeting page. I've updated the question with my full code and a diagram explaining what I am trying to do.
I'll start with B:
The error indicates that the greeting value is None, leading to an exception when jinja2 attempts to expand greeting.key in {{ greeting.key.urlsafe() }} during template rendering.
One option is to re-arrange the handler code to prevent rendering that template if the necessary conditions are not met, maybe something along these lines:
...
greeting = Greeting.get_by_id(args[0])
if not greeting or not isinstance(greeting.key, ndb.Key):
# can't render that template, invalid greeting.key.urlsafe()
webapp2.abort(500)
return
template_values = {
'greeting': greeting,
}
template = JINJA_ENVIRONMENT.get_template('detail.html')
self.response.write(template.render(template_values))
...
Alternatively you can wrap the template areas referencing the variables with appropriate checks (IMHO uglier, harder and more fragile, tho - python is way better for this kind of logic than jinja2), something along these lines:
{% if greeting and greeting.key %}<img src="/img?avatar_img_id={{ greeting.key.urlsafe() }}">{% endif %}
Now to A.
In short - not a great idea, primarily because the numeric ID you're trying to use in the URL is not unique except for greetings under the same parent entity!. Which in a way explains why greeting is invalid leading to the error from the B answer.
The greeting = Greeting.get_by_id(args[0]) will return None unless you also created a greeting entity with the ID you're passing in args[0] and no parent!
In order to obtain by ID the greeting you created with:
greeting = Greeting(parent=guestbook_key(guestbook_name))
you'd need to call:
greeting = Greeting.get_by_id(args[0],
parent=guestbook_key(guestbook_name))
You could, if you want to continue in the same direction, also encode the guestbook_name in the greeting URL, which would allow you to also obtain the needed parent key, maybe something along these lines:
/guestbook/<guestbook_name>/greeting/<numeric-id>.
You also need to take a closer look at the image handling. You have architectural inconsistencies: in the diagram and the model you have both the avatar and the other image attached to a single greeting entity, but in the Image handler each of them is attached to a separate greeting. The handler also doesn't map at all to the images' URLs (which also needs additional encodings for the data you need to locate the appropriate image, depending on the architectural decision).
I'm afraid you still have a lot of work to do until you get the entire thing to work, much more than properly fit for a single SO question. Step back, re-think your architecture, split it into smaller pieces, focus on one piece at a time and get it going. After you become familiar with the technology for the various pieces you'll feel better tackling the whole problem at once.

HTML Page Loads, but shows up blank

I'm working on a project in Python, and I have part of the project working (where the user submits a post). I'm trying to make it so that when the user submits their entry, they get redirected to another page, which shows all the things they've posted. When I test this, I get redirected to the new page I made, but the page is blank. Here is my code:
class Handler(webapp2.RequestHandler):
def write(self, *a, **kw):
self.response.out.write(*a, **kw)
def render_str(self, template, **params):
t = jinja_env.get_template(template)
return t.render(params)
def render(self, template, **kw):
self.write(self.render_str(template, **kw))
class Entry(db.Model):
subject = db.StringProperty(required=True)
entry = db.TextProperty(required=True)
created = db.DateTimeProperty(auto_now_add = True)
class MainPage(Handler):
def render_front(self, subject="", entry="", error=""):
blog = db.GqlQuery("SELECT * FROM Entry ORDER BY created DESC LIMIT 10")
self.render("entry.html", subject=subject, entry=entry, error=error, blog=blog)
def get(self):
self.render_front()
def post(self):
subject = self.request.get("subject")
entry = self.request.get("entry")
if subject and entry:
e = Entry(subject = subject, entry=entry)
e.put()
self.redirect("/BlogPost")
else:
error = "To post a new entry, you must add both, a subject and your post"
self.render_front(subject, entry, error)
class BlogPost(Handler):
def get(self):
self.render("blogfront.html")
app = webapp2.WSGIApplication([('/', MainPage), ('/BlogPost', BlogPost)], debug = True)
This is just a piece of my code (I believe the error lies somewhere along those lines since my front page is working).
This is my blogfront.html:
<!DOCTYPE html>
<html>
<head>
<title>Blog </title>
</head>
<body>
{% for entry in blog %}
<div class="entry">
<div class="entry-subject">{{entry.subject}}</div>
<label>{{entry.created}}</label>
<hr>
<pre class="entry-body">{{entry.entry}}</pre>
</div>
{% endfor %}
</body>
</html>
entry.html is loading while blogfront.html is not. I'm not sure where I'm going wrong with this. I would appreciate any help. Thanks in advance.
Going by your comments to questioners the issue is that while you define blog in the render_front() method, it's a local variable so it vanishes when the method returns. Try retrieving the data again in your BlogPost() method and pass that as the blog argument to self.render(). Without any blog data the template will indeed render as empty.
So your updated method might read:
class BlogPost(Handler):
def get(self):
blog = db.GqlQuery("SELECT * FROM Entry ORDER BY created DESC LIMIT 10")
self.render("blogfront.html", blog=blog)
assuming that you want to see the same data you display in MainPage(), but you may well want something else.

method="delete" from html form in FLASK

I have an HTML form:
{% set delete_urls = url_for('store_add') ~ store_id ~ "?__METHOD_OVERRIDE__=DELETE" %}
<form action="{{delete_urls}}" name="delete" method="post" id="{{form_id}}" style="display:none">
and in the views:
class StoreAdd(MethodView):
#login_required
def delete(self,store_id):
store_selected = request.args['store_id']
qstr = "DELETE FROM store WHERE store_id=%d AND cust_id=%d"%(store_id,self.cust_id)
h = pgexec(qstr,False,True)
h.process()
flash("deleted the store:%d"%(store_selected))
return redirect(url_for('store_add'))
store_add = StoreAdd.as_view('store_add')
app.add_url_rule('/storeadd/',
defaults={'store_id': 0},
view_func=store_add,
methods=["GET","PUT"])
app.add_url_rule('/storeadd/',
view_func=store_add,
methods=["POST"])
app.add_url_rule('/storeadd/<int:store_id>',
view_func=store_add,
methods=['DELETE','PUT','GET'])
Of course implemented the routing:
from werkzeug import url_decode
from flask import flash
class MethodRewriteMiddleware(object):
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
if 'METHOD_OVERRIDE' in environ.get('QUERY_STRING', ''):
args = url_decode(environ['QUERY_STRING'])
method = args.get('__METHOD_OVERRIDE__')
if method in ['GET', 'POST', 'PUT', 'DELETE']:
method = method.encode('ascii', 'replace')
environ['REQUEST_METHOD'] = method
return self.app(environ, start_response)
But still on submission of the delete form it cannot access the delete method?
What is going wrong?
Edit:
The problem with delete is as follows.
When I submit the form it seems it tries to "POST" to url:
/storeadd/13?__METHOD_OVERRIDE__=DELETE
But the POST url rule says it can only be: /storeadd. Thus it gives 405 ERROR Page.
Thus the override which should happen never happens.
Have you applied this middleware on your flask application?
app.wsgi_app = MethodRewriteMiddleware(app.wsgi_app)

Implementing breadcrumbs in Python using Flask?

I want breadcrumbs for navigating my Flask app. An option could be to use a general Python module like bread.py:
The bread object accepts a url string and grants access to the url
crumbs (parts) or url links (list of hrefs to each crumb) .
bread.py generates the breadcrumb from the url path, but I want the elements of the breadcrumb to be the title and link of the previously visited pages.
In Flask, maybe this can be done using a decorator or by extending the #route decorator.
Is there a way to have each call of a route() add the title and link of the page (defined in the function/class decorated with #route) to the breadcrumb? Are there other ways to do it? Any examples of breadcrumbs implemented for Flask?
So you're after "path/history" breadcrumbs, rather than "location" breadcrumbs to use the terminology from the wikipedia article?
If you want to have access to the user's history of visited links, then you're going to have to save them in a session. I've had a go at creating a decorator to do this.
breadcrumb.py:
import functools
import collections
import flask
BreadCrumb = collections.namedtuple('BreadCrumb', ['path', 'title'])
def breadcrumb(view_title):
def decorator(f):
#functools.wraps(f)
def decorated_function(*args, **kwargs):
# Put title into flask.g so views have access and
# don't need to repeat it
flask.g.title = view_title
# Also put previous breadcrumbs there, ready for view to use
session_crumbs = flask.session.setdefault('crumbs', [])
flask.g.breadcrumbs = []
for path, title in session_crumbs:
flask.g.breadcrumbs.append(BreadCrumb(path, title))
# Call the view
rv = f(*args, **kwargs)
# Now add the request path and title for that view
# to the list of crumbs we store in the session.
flask.session.modified = True
session_crumbs.append((flask.request.path, view_title))
# Only keep most recent crumbs (number should be configurable)
if len(session_crumbs) > 3:
session_crumbs.pop(0)
return rv
return decorated_function
return decorator
And here's a test application that demonstrates it. Note that I've just used Flask's built-in client side session, you'd probably want to use a more secure server-side session in production, such as Flask-KVsession.
#!/usr/bin/env python
import flask
from breadcrumb import breadcrumb
app = flask.Flask(__name__)
#app.route('/')
#breadcrumb('The index page')
def index():
return flask.render_template('page.html')
#app.route('/a')
#breadcrumb('Aardvark')
def pagea():
return flask.render_template('page.html')
#app.route('/b')
#breadcrumb('Banana')
def pageb():
return flask.render_template('page.html')
#app.route('/c')
#breadcrumb('Chimp')
def pagec():
return flask.render_template('page.html')
#app.route('/d')
#breadcrumb('Donkey')
def paged():
return flask.render_template('page.html')
if __name__ == '__main__':
app.secret_key = '83cf5ca3-b1ee-41bb-b7a8-7a56c906b05f'
app.debug = True
app.run()
And here's the contents of templates/page.html:
<!DOCTYPE html>
<html>
<head><title>{{ g.title }}</title></head>
<body>
<h1>{{ g.title }}</h1>
<p>Breadcrumbs:
{% for crumb in g.breadcrumbs %}
{{ crumb.title }}
{% if not loop.last %}ยป{% endif %}
{% endfor %}
</p>
<p>What next?</p>
<ul>
<li>Aardvark?</li>
<li>Banana?</li>
<li>Chimp?</li>
<li>Donkey?</li>
</ul>
</body>
</html>
i was trying to use the breadcrumb.py , but i was need to check:
if the new item "item = (flask.request.path, view title) " is already exist in the session crumbs, then i will delete all other items frome the index to the end, i do this for Avoid repetition in my session crumds.
flask.session.modified = True
item = (flask.request.path, view_title)
try:
if not item in session_crumbs:
session_crumbs.append(item)
else:
index = session_crumbs.index(item)
session_crumbs = session_crumbs[:index+1]
except:
pass
return rv
return decorated_function
return decorator

Uploading a video to google app engine blobstore

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)

Categories