My first Google App Engine/Python app - python

I am trying to write my first GAE/Python application that does the following three things:
Displays a form where the user can enter details about themself (index.html)
Stores the submitted form data in the datastore
Retrieves all data from datastore and displays all results above the form (index.html)
However, I'm getting the following error
line 15, in MainPage 'people' : people NameError: name 'people' is not
defined
Any advice on how to resolve this and get my app working will be appreciated!
main.py
import webapp2
import jinja2
import os
jinja_environment = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
class MainPage(webapp2.RequestHandler):
def get(self):
people_query = Person.all()
people = people_query.fetch(10)
template_values = {
'people': people
}
template = jinja_environment.get_template('index.html')
self.response.out.write(template.render(template_values))
# retrieve the submitted form data and store in datastore
class PeopleStore(webapp2.RequestHandler):
def post(self):
person = Person()
person.first_name = self.request.get('first_name')
person.last_name = self.request.get('last_name')
person.city = self.request.get('city')
person.birth_year = self.request.get('birth_year')
person.birth_year = self.request.get('height')
person.put()
# models a person class
class Person(db.Model):
first_name = db.StringProperty()
last_name = db.StringProperty()
city = db.StringProperty()
birth_year = db.IntegerProperty()
height = db.IntegerProperty()
app = webapp2.WSGIApplication([('/', MainPage),
('/new_person')], debug=True)
index.html
<html>
<body>
{% for person in people %}
{% if person %}
<b>{{ person.first_name }}</b>
<b>{{ person.last_name }}</b>
<b>{{ person.city }}</b>
<b>{{ person.birth_year }}</b>
<b>{{ person.height }}</b>
<hr></hr>
{% else %}
No people found
{% endfor %}
<form action="/new_person" method="post">
<div><textarea name="first_name" rows="3" cols="60"></textarea></div>
<div><textarea name="last_name" rows="3" cols="60"></textarea></div>
<div><textarea name="city" rows="3" cols="60"></textarea></div>
<div><textarea name="birth_year" rows="3" cols="60"></textarea></div>
<div><textarea name="height" rows="3" cols="60"></textarea></div>
<div><input type="submit" value="Submit"></div>
</form>
</body>
</html>
app.yaml
application: some_name
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
script: main.app
libraries:
- name: jinja2
version: latest
EDIT 1
*main.py*
import webapp2
import jinja2
import os
from google.appengine.ext import db
jinja_environment = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
class MainPage(webapp2.RequestHandler):
def get(self):
people_query = Person.all()
people = people_query.fetch(10)
template_values = {
'people': people
}
template = jinja_environment.get_template('index.html')
self.response.out.write(template.render(template_values))
class PeopleStore(webapp2.RequestHandler):
def post(self):
person = Person()
person.first_name = self.request.get('first_name')
person.last_name = self.request.get('last_name')
person.city = self.request.get('city')
person.birth_year = self.request.get('birth_year')
person.height = self.request.get('height')
person.put()
class Person(db.Model):
first_name = db.StringProperty()
last_name = db.StringProperty()
city = db.StringProperty()
birth_year = db.IntegerProperty()
height = db.IntegerProperty()
app = webapp2.WSGIApplication([('/', MainPage),
('/new_person')], debug=True)
EDIT 2
*main.py*
The following edit fixed this error
AttributeError: 'str' object has no attribute 'get_match_routes'
app = webapp2.WSGIApplication([('/', MainPage),('/new_person',PeopleStore)], debug=True)
Ok great, the form displays in the browser, but when I submit the data, I get this error:
BadValueError: Property birth_year must be an int or long, not a
unicode
EDIT 3
main.py
person.birth_year = int(self.request.get('birth_year'))
person.height = int(self.request.get('height'))
resolved this error:
badvalueerror property must be an int or long, not a unicode
Ok, good so far. The data stores in the data store. However, my page comes up blank...

You have an indentation problem. Lines 3 and onwards of your get method should be indented at the same level as the first two lines. Otherwise, they are not part of the method, but the class definition itself, and will be executed when the class is defined - at which point there is no variable people in scope.

In your app.yaml it doesnt like the underscore
#application: some_name
application: somename
There are some more issues with blocks not being closed etc which you need to work through yourself

Related

CSS failing to load on app engine python/jinja2 guestbook tutorial

Couldn't find anything on Stack Overflow. I've made sure that all my code and file structure is exactly verbatim from the tutorial here: https://developers.google.com/appengine/docs/python/gettingstartedpython27/staticfiles
My site: nathanwjclark.appspot.com
The error I get: http://i.imgur.com/oH70Jd7.png
How can I get my CSS to load correctly?
App.yaml:
application: nathanwjclark
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /stylesheets
static_dir: stylesheets
- url: /.*
script: guestbook.application
libraries:
- name: webapp2
version: latest
- name: jinja2
version: latest
index.yaml:
indexes:
- kind: Greeting
ancestor: yes
properties:
- name: date
direction: desc
index.html:
<!DOCTYPE html>
{% autoescape true %}
<html>
<head> <link type="text/css" rel="stylesheet" href="/stylesheets/main.css" /> </head> <body>
{% for greeting in greetings %}
{% if greeting.author %}
<b>{{ greeting.author.nickname() }}</b> wrote:
{% else %}
An anonymous person wrote:
{% endif %}
<blockquote>{{ greeting.content }}</blockquote>
{% endfor %}
<form action="/sign?guestbook_name={{ guestbook_name }}" method="post">
<div><textarea name="content" rows="3" cols="60"></textarea></div>
<div><input type="submit" value="Sign Guestbook"></div>
</form>
<hr>
<form>Guestbook name:
<input value="{{ guestbook_name }}" name="guestbook_name">
<input type="submit" value="switch">
</form>
{{ url_linktext }}
</body>
</html>
{% endautoescape %}
guestbook.py:
# [START imports]
import os
import urllib
from google.appengine.api import users
from google.appengine.ext import ndb
import jinja2
import webapp2
JINJA_ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=['jinja2.ext.autoescape'],
autoescape=True)
# [END imports]
DEFAULT_GUESTBOOK_NAME = 'default_guestbook'
# We set a parent key on the 'Greetings' to ensure that they are all in the same
# entity group. Queries across the single entity group will be consistent.
# However, the write rate should be limited to ~1/second.
def guestbook_key(guestbook_name=DEFAULT_GUESTBOOK_NAME):
"""Constructs a Datastore key for a Guestbook entity with guestbook_name."""
return ndb.Key('Guestbook', guestbook_name)
class Greeting(ndb.Model):
"""Models an individual Guestbook entry with author, content, and date."""
author = ndb.UserProperty()
content = ndb.StringProperty(indexed=False)
date = ndb.DateTimeProperty(auto_now_add=True)
# [START main_page]
class MainPage(webapp2.RequestHandler):
def get(self):
guestbook_name = self.request.get('guestbook_name',
DEFAULT_GUESTBOOK_NAME)
greetings_query = Greeting.query(
ancestor=guestbook_key(guestbook_name)).order(-Greeting.date)
greetings = greetings_query.fetch(10)
if users.get_current_user():
url = users.create_logout_url(self.request.uri)
url_linktext = 'Logout'
else:
url = users.create_login_url(self.request.uri)
url_linktext = 'Login'
template_values = {
'greetings': greetings,
'guestbook_name': urllib.quote_plus(guestbook_name),
'url': url,
'url_linktext': url_linktext,
}
template = JINJA_ENVIRONMENT.get_template('index.html')
self.response.write(template.render(template_values))
# [END main_page]
class Guestbook(webapp2.RequestHandler):
def post(self):
# We set the same parent key on the 'Greeting' to ensure each Greeting
# is in the same entity group. Queries across the single entity group
# will be consistent. However, the write rate to a single entity group
# should be limited to ~1/second.
guestbook_name = self.request.get('guestbook_name',
DEFAULT_GUESTBOOK_NAME)
greeting = Greeting(parent=guestbook_key(guestbook_name))
if users.get_current_user():
greeting.author = users.get_current_user()
greeting.content = self.request.get('content')
greeting.put()
query_params = {'guestbook_name': guestbook_name}
self.redirect('/?' + urllib.urlencode(query_params))
application = webapp2.WSGIApplication([
('/', MainPage),
('/sign', Guestbook),
], debug=True)
You mistakenly saved it as main.css.txt and it is served at:
http://nathanwjclark.appspot.com/stylesheets/main.css.txt

Ancestor Query not displaying results

I'm having trouble getting my ancestor queries to display the links associated with Tom's photos that are stored in the datastore. Nothing is displayed from the datastore even though there are several links in the datastore. Any assistance will be appreciated. Thanks in advance.
main.py
import webapp2
import jinja2
import os
from google.appengine.ext import db
jinja_environment = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
class MainPage(webapp2.RequestHandler):
def get(self):
tom = Person(key_name='Tom')
wedding_photo = Photo(parent=tom)
wedding_photo.image_url='http://pinterest.com/pin/200691727117011230/'
wedding_photo.put()
baby_photo = Photo(parent=tom)
baby_photo.image_url='http://pinterest.com/pin/518828819542052681/'
baby_photo.put()
dance_photo = Photo(parent=tom)
dance_photo.image_url='http://pinterest.com/pin/257197828689352707/'
dance_photo.put()
dog_photo = Photo()
dog_photo.image_url='http://pinterest.com/pin/279575089339614779/'
dog_photo.put()
photo_query = Photo.all()
photo_query.ancestor(tom)
message = "Photos"
template_values = {
'message': message,
'photo_query': photo_query,
}
template = jinja_environment.get_template('index.html')
self.response.out.write(template.render(template_values))
class PhotoStore(webapp2.RequestHandler):
def post(self):
photo = Photo()
photo.image_url = self.request.get('image_url')
photo.put()
self.redirect('/')
class Person(db.Model):
name = db.StringProperty()
class Photo(db.Model):
image_url = db.StringProperty()
app = webapp2.WSGIApplication([('/', MainPage),
('/new_photo',PhotoStore)], debug=True)
index.html
<html>
<body>
<form action="/new_photo" method="post">
<label for="photo">Photo</label>
<div><textarea name="image_url" rows="1" cols="60" id="image_url"></textarea></div>
<input type="submit" value="Submit">
</form>
<hr></hr>
<div><b>{{ message }}</b></div>
<b>{% for p in photo_query.run(limit=5): %}</b>
<div>{{ p }}</div>
<b>{% endfor %}</b>
</body>
</html>
What do you see if you open the generated html source? I assume that the code {{ p }} just shows string representation of the Photo objects, which results in showing strings like <__main__.Photo object at 0xfcbd9ef0> that is interpreted as HTML tag and doesn't show in the rendered HTML.

How to handle delete in Google App Engine (Python)

I'm a newbie programmer and new to Google App Engine and webapp2 etc. So this may be a very basic question.
I am creating an application to store images into BlobStore. My model stores description, blob_key, image url and date.
I am able to save everything, so that bit is okay.
But now I want to create a delete button which will not only delete an item from the datastore, but also delete the image saved in the blobstore.
I have created a DeleteHandler, and in the html I have a form, passing the key for the item I want to delete. In the DeleteHandler, I am using the posted key to delete the item from the datastore. I am also trying to use the key to use it delete the image saved in the blobstore.
So far I'm getting a 404 on the delete form post, and even if I get past that, I'm not sure if my DeleteHandler is correct to handle the functionality I am looking for.
Any help would be much appreciated..
Main.py:
import os
import urllib
import webapp2
from google.appengine.ext.webapp import template
from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers
from google.appengine.api import images
#Models
from google.appengine.ext import db
class ImageItem(db.Model):
description = db.StringProperty(required=True)
img_url = db.StringProperty()
blob_key = blobstore.BlobReferenceProperty()
when = db.DateTimeProperty(auto_now_add=True)
#Handlers (Views)
class MainHandler(webapp2.RequestHandler):
def get(self):
upload_url = blobstore.create_upload_url('/upload')
imgs = db.GqlQuery(
'SELECT * FROM ImageItem '
'ORDER BY when DESC')
imgs_dict = {'imgs': imgs}
self.response.out.write( template.render( 'main.html',locals() ) )
class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
f = self.get_uploads('file')[0] # 'file' is file upload field in the form
img =ImageItem(description=self.request.get('description'))
img.blob_key = f.key()
img.img_url = images.get_serving_url( f.key() )
img.put()
self.redirect('/')
class DeleteHandler(webapp2.RequestHandler):
def post(self):
key = self.request.get('k')
item = db.get(key)
images.delete( item.blob_key )
item.delete()
self.response.out.write(key)
#URL Routing happens here
app = webapp2.WSGIApplication([('/', MainHandler),
('/upload', UploadHandler),
('/delete', DeleteHandler)],
debug=True)
Main.html:
<form action="{{upload_url}}" method="POST" enctype="multipart/form-data">
<p>
<label for="file">Upload File</label>
<input type="file" name="file" id="file">
</p>
<p>
<label for="description">Description</label>
<input type="text" id="description" name="description">
</p>
<input type="submit" name="submit" value="Submit">
</form>
<ul>
{% for i in imgs %}
<li>
<img src="{{i.img_url}}=s400-c" alt="">
{{i.description }}
</li>
<li>{{i.when }}</li>
<li>
<form action="/delete" method="POST" enctype="multipart/form-data">
<input type="text" name="k" value="{{i.key}}" />
<input type="submit" value="delete">
</form>
</li>
{% endfor %}
</ul>
The deletes are close. Once you have a key, you can delete entities by calling db.delete(key).
For your example, this would be something like this:
class DeleteHandler(webapp2.RequestHandler):
def post(self):
key = self.request.get('k')
item = db.get(key)
blobstore.delete([item.blob_key])
db.delete(item)
self.response.out.write(key)
Your url handling in main.py is good, so it's not obvious to me why you're getting a 404. You could double-check your app.yaml file to make sure all urls are passed to main.py.
Here's a sample app.yaml handlers url section:
handlers
- url: /.*
script: main.app
Got it to work. Thanks Eric, yours was real close. I needed to use blob_key.key().
Final code is following:
class DelHandler(webapp2.RequestHandler):
def post(self):
key = self.request.get('k')
item = db.get(key)
n = item.blob_key.key()
blobstore.delete(n)
item.delete()
self.redirect('/')

Internal Server Error - Google App Server (Property Content Required)

I keep receiving an internal server error - 'BadValueError: Property content is required. It seems to happen when I pass blog_table into the template to render. Here's the code:
main.py:
import os
import webapp2
import jinja2
from google.appengine.ext import db
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__),'templates')), 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))
class Blog(db.Model):
subject = db.StringProperty(required = True)
content = db.TextProperty(required = True)
created = db.DateTimeProperty(auto_now_add = True)
class MainPage(Handler):
def render_front(self, subject="", content="", error=""):
blog_table = db.GqlQuery("SELECT * FROM Blog ORDER BY created DESC")
self.render("blog.html",subject = subject,content = content,error = error,blog_table = blog_table)
def get(self):
self.render_front()
def post(self):
subject = self.request.get("subject")
content = self.request.get("content")
if subject and content:
b = Blog(subject = subject, content = content)
b.put()
self.redirect("/")
else:
error = "we need both a subject and some content"
self.render_front(subject,content,error)
app = webapp2.WSGIApplication([('/', MainPage)],debug=True)
HTML:
<!DOCTYPE html>
<html>
<head>
<title>Blog</title>
</head>
<body>
<h1>Blog</h1>
<form method="post">
<label>
<div>Subject</div>
<input type="text" name="subject" value="{{subject}}">
</label>
<label>
<div>Content</div>
<textarea name="content" value="{{content}}"></textarea>
</label>
<div class="error">{{error}}</div>
<input type="submit">
</form>
<hr>
{% for post in blog_table %}
<div class="post">
<div class="subject">{{post.subject}}</div>
<pre class="content">{{post.content}}</pre>
</div>
{% endfor %}
</body>
</html>
To see the error go to http://udacity-cs253-a31.appspot.com/
Thanks.
This is just a guess - changed your Blog model and added the Content attribute recently.
There's some old instances of the Blog model in your datastore that have no Content. If you try to load those, your code fails. If you load a Blog instance with Content, everything is fine.
You have to go through your datastore and ensure all instances of your Blog have Content.

HTTP post with GAE (and WTForms)

Hi how can I get the variables in a HTTP post with WTForms when the post is done with a blobstoreuploadhandler and preferably also with i18n localized messages for validation?
This is my code that is not working:
class AdForm(Form):
name = TextField(_('Name'))
title = TextField(_('title'))
text = TextAreaField(_('Text'),widget=TextArea())
phonenumber = TextField(_('Phone number'))
phonenumberhide = BooleanField(_('Display phone number on site'))
price = TextField(_('Price'))
password = PasswordField(_('Password'))
email = TextField(_('Email'))
When I try to access the data posted via the form the data turns out as None:
form = AdForm(data=self.request.POST)
if form.title:
logging.info('getting title:'+form.title.data)
ad.title = form.title.data
ad.save()
The above does not save anything to the datastore and this is the template where it's coming from
<div class="labelform">
<div class="labelform" style="clear:left;">
<label> {% filter capitalize %}{% trans %}title{% endtrans %}{% endfilter %}:</label>
</div>
</div>
</td><td>
{{ form.title }}{% if form.title.errors %}
<ul class="errors">{% for error in form.title.errors %}<li>{{ error }}</li>{% endfor %}</ul>
{% endif %}
Can you help me? There's something in the WTForms manual about appengine but I couldn't find a working example.
Update
I added validation tests and I still can't access the variables:
logging.info('getting requests')
if form.validate():
if form.title:
logging.info('getting title:'+form.title.data)
ad.title = form.title.data
ad.save()
ad.put()
Logging output:
INFO 2011-11-05 23:17:24,653 main.py:1504] getting requests INFO
2011-11-05 23:17:24,653 main.py:1507] getting title:
Update 2
I removed the WTForms dependence and it is still not working. The line logging.info('getting data:'+ self.request.get('title', '0')) only outputs 0 even though the form is just a regular http post form:
<form action="{{form_url}}" name="upload" method="post" enctype="multipart/form-data" accept-charset="utf-8">
Update 3
This minimal config with no WTForms and no Jinja works so it's probably something with Jinja when this bare-bones example works with webapp2 and python 2.7 where I'm going to add the faulty code line by line to troubleshoot:
class GuestPage(BaseHandler):
def get(self):
self.response.out.write("""
<html>
<body>
<form action="/sign" method="post">
<div><textarea name="content" rows="3" cols="60"></textarea></div>
<div><input type="submit" value="Sign Guestbook"></div>
</form>
</body>
</html>""")
class Guestbook(BaseHandler, I18NHandler, blobstore_handlers.BlobstoreUploadHandler):
csrf_protect = False
def post(self):
self.response.out.write('<html><body>You wrote:<pre>')
self.response.out.write(self.request.get('content'))
self.response.out.write('</pre></body></html>')
app = webapp2.WSGIApplication([ ('/guest', GuestPage),
('/sign', Guestbook),
...
Update 4
My back to basics is working with Jinja so I suppose I just build on this example and see where it breaks:
class GuestPage(BaseHandler):
def get(self):
self.render_jinja('form_jinja')
class Guestbook(BaseHandler, I18NHandler, blobstore_handlers.BlobstoreUploadHandler):
csrf_protect = False
def post(self):
self.response.out.write('<html><body>You wrote:<pre>')
self.response.out.write(self.request.get('content'))
self.response.out.write('</pre></body></html>')
Update 5
I can reproduce the error with this minimal example that can't access the http post variable:
class GuestPage(webapp2.RequestHandler):
def get(self):
self.response.out.write("""
<html>
<body>
<form action=" """ +blobstore.create_upload_url('/sign')+ """ " method="post">
<div><textarea name="content" rows="3" cols="60"></textarea></div>
<div><input type="submit" value="Sign Guestbook"></div>
</form>
</body>
</html>""")
class Guestbook(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
self.response.out.write('<html><body>You wrote:<pre>')
self.response.out.write(self.request.get('content'))
self.response.out.write('</pre></body></html>')
app = webapp2.WSGIApplication([ ('/guest', GuestPage),
('/sign', Guestbook),
Update 6
From the guestbook example code with blobstoreuploadhandler I can upload a file on the production server so I could make a working example that uses the blobstoreuploadhandler that I will try to build on for my use case.
Update 7
I could get my original code so that everything works except the blob transfer. I suspect a diff between dev_appserver and production that I posted to the google appengine group about. We'll see how it progresses.
Update 8
Here's another common use how nothing works when you add WTForms:
logging.info('getting data:'+ self.request.get('title', '0'))
logging.info('http post data:'+ str(self.request.post))
form = AdForm(formdata=self.request.data)
logging.info('populated form')
logging.info('form data:' + str(form.formdata))
if form.validate():
if form.title:
logging.info('getting title:'+str( form.get('title') ) )
ad.title = form.title.data ad.save() ad.put()
if form.text:
logging.info('getting text:' +str(form.text))
ad.text = form.text.data
if self.request.get('currency'):
ad.currency = self.request.get('currency')
if self.request.get('cg'):
ad.category = form.cg.data
if self.request.get('company_ad') == '1':
ad.company_ad = True
ad.put()
else:
logging.info('form did not validate')
except Exception, ex:
logging.info('there occured exception %s', str(ex))
INFO 2011-11-09 12:11:50,868 main.py:1385] getting data:TEST INFO
2011-11-09 12:11:50,868 main.py:1409] there occured exception post
Update 9
Finally the form populates it just doesn't validate. Thank you Sean for the info that got me further. Now I get past populated the form object with no exception but the exception occurs when I try to validate:
logging.info('getting data:'+ self.request.get('title', '0'))
form = AForm(self.request.POST)
logging.info('populated form')
if form.validate():
logging.info('validated form')
The above code is logging the output:
INFO 2011-11-11 08:03:59,913 main.py:1387] getting data:TEST
INFO 2011-11-11 08:03:59,914 main.py:1390] populated form
INFO 2011-11-11 08:03:59,914 main.py:1412] there occured exception 'builtin_function_or_method' object is not iterable
What does the exception mean?
My form class is
class AForm(Form):
name = TextField(_('Name'))
title = TextField(_('title'))
text = TextAreaField(_('Text'),widget=TextArea())
phonenumber = TextField(_('Phone number'))
phonenumberhide = BooleanField(_('Display phone number on site'))
price = TextField(_('Price'))
password = PasswordField(_('Password'))
email = TextField(_('Email'))
category = SelectField(choices=categories.keys)
I don't know anything about WTForm, but I'd guess that like Django forms, you need to call the validation function before you can access the data. In this case, it's form.validate():
form = AdForm(formdata=self.request.POST)
if form.validate():
ad.title = form.title.data
Daniel actually it is not data=self.request.POST that you need to pass to the form but formdata instead of data
http://wtforms.simplecodes.com/docs/dev/forms.html#the-form-class
hope it will be usefull for all those who rushed through the doc as i did

Categories

Resources