開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python (プログラミング言語)
Head First Python (Paul Barry(著)、 O'Reilly Media )のChapter 10(Scaling your Webapp: Getting real)、EXERCISE(p.381)を解いてみる。
EXERCISE(p.381)
コード(BBEdit, Emacs)
hfwwg.py
from google.appengine.ext import webapp from google.appengine.ext.webapp.util import run_wsgi_app from google.appengine.ext import db from google.appengine.ext.webapp import template from google.appengine.ext.db import djangoforms import hfwwgDB class SightingForm(djangoforms.ModelForm): class Meta: model = hfwwgDB.Sighting class SightingInputPage(webapp.RequestHandler): def get(self): html = template.render('templates/header.html', {'title': 'Report a Possible Sighting'}) html += template.render('templates/form_start.html', {}) html += str(SightingForm(auto_id=False)) html += template.render('templates/form_end.html', {'sub_title':'Submit Sighting'}) html += template.render('templates/footer.html', {'links':''}) self.response.out.write(html) # exercise def post(self): new_sighting.sighting = hfwwgDB.Sighting() new_sighting.name = self.request.get('name') new_sighting.email = self.request.get('email') new_sighting.date = self.request.get('date') new_sighting.time = self.request.get('time') new_sighting.location = self.request.get('location') new_sighting.fin_type = self.request.get('fin_type') new_sighting.whale_type = self.request.get('whale_type') new_sighting.blow_type = self.request.get('blow_type') new_sighting.wave_type = self.request.get('wave_type') new_sighting.put() html = template.request('templates/header.html', {'title': 'Thank you'}) html += '<p>Thank you for providing your sighting data.</p>' html += template.render( 'templates/footer.html', {'links':'Enter <a href="/">another sighting</a>.'}) self.response.out.write(html) # exercise end app = webapp.WSGIApplication({('/.*', SightingInputPage)}, debug=True) def main(): run_wsgi_app(app) if __name__ == '__main__': main()
0 コメント:
コメントを投稿