Ok, you guys were quick and helpful last time so I'm going back to the well ;)
Disclaimer: I'm new to python and very new to App Engine. What I'm trying to do is a simple modification of the example from the AppEngine tutorial.
I've got my date value being stored in my Memory class:
class Memory(db.Model):
author = db.UserProperty()
content = db.StringProperty(multiline=True)
date = db.DateProperty(auto_now_add=True)
and now I want to be able to lookup records for a certain date. I wasn't sure exactly how to do it so I tried a few things including:
memories = db.GqlQuery("SELECT * from Memory where date = '2007-07-20')
and
memories = Memory.all()
memories.filter("date=", datetime.strptime(self.request.get('date'), '%Y-%m-%d').date())
and
memories = Memory.all()
memories.filter("date=", self.request.get('date'))
But every time I run it, I get an ImportError. Frankly, I'm not even sure how to parse these error message I get when the app fails but I'd just be happy to be able to look up the Memory records for a certain date.
EDIT: FULL SOURCE BELOW
import cgi
import time
from datetime import datetime
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
class Memory(db.Model):
author = db.UserProperty()
content = db.StringProperty(multiline=True)
date = db.DateProperty()
class MainPage(webapp.RequestHandler):
def get(self):
self.response.out.write('<html><body>')
memories = db.GqlQuery('SELECT * from Memory ORDER BY date DESC LIMIT 10')
for memory in memories:
self.response.out.write('<b>%s</b> wrote: ' % memory.author.nickname())
self.response.out.write('<em>%s</em>' % memory.date)
self.response.out.write('<blockquote>%s</blockquote>' % cgi.escape(memory.content))
self.response.out.write("""
<div style="float: left;">
<form action="/post" method="post">
<fieldset>
<legend>Record</legend>
<div><label>Memory:</label><input type="text" name="content" /></textarea></div>
<div><label>Date:</label><input type="text" name="date" /></div>
<div><input type="submit" value="Record memory" /></div>
</fieldset>
</form>
</div>
<div style="float: left;">
<form action="/lookup" method="post">
<fieldset>
<legend>Lookup</legend>
<div><label>Date:</label><input type="text" name="date" /></div>
<div><input type="submit" value="Lookup memory" /></div>
</fieldset>
</form>
</div>""")
self.response.out.write('</body></html>')
class PostMemory(webapp.RequestHandler):
def post(self):
memory = Memory()
if users.get_current_user():
memory.author = users.get_current_user()
memory.content = self.request.get('content')
memory.date = datetime.strptime(self.request.get('date'), '%Y-%m-%d').date()
memory.put()
self.redirect('/')
class LookupMemory(webapp.RequestHandler):
def post(self):
memories = db.GqlQuery("SELECT * FROM Memory WHERE date = '2009-07-21'")
for memory in memories:
self.response.out.write('<b>%s</b> wrote: ' % memory.author.nickname())
self.response.out.write('<em>%s</em>' % memory.date)
self.response.out.write('<blockquote>%s</blockquote>' % cgi.escape(memory.content))
application = webapp.WSGIApplication([('/', MainPage), ('/post', PostMemory), ('/lookup', LookupMemory)], debug=True)
def main():
run_wsgi_app(application)
if __name__ == '__main__':
main()