Python Web Development

Slides:



Advertisements
Similar presentations
Months of the year December January November October February
Advertisements

Web Basics Willem Visser RW334. Overview Basic Browser and Server Interaction – Forms with actions, url encoding and handlers – Parameters – Having more.
Chubaka Producciones Presenta :.
2012 JANUARY Sun Mon Tue Wed Thu Fri Sat
P Pathophysiology Calendar. SundayMondayTuesdayWednesdayThursdayFridaySaturday January 2012.
Chicas, este calendario si es pa' nosotras !!!!!.
MONDAYTUESDAYWEDNESDAYTHURSDAYFRIDAYSAT/SUN Note: You can print this template to use as a wall calendar. You can also copy the slide for any month to add.
You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation. If you’d like to change.
School Year Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation.
School Year Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation.
You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation. If you’d like to change.
2007 Monthly Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation.
MONDAYTUESDAYWEDNESDAYTHURSDAYFRIDAYSAT/SUN Note: You can print this template to use as a wall calendar. You can also copy the slide for any month to add.
January 2013 MondayTuesdayWednesdayThursdayFridaySaturdaySunday.
You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation. If you’d like to change.
You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation. If you’d like to change.
Cloud computing lectures: Programming with Google App Engine Keke Chen.
WORD JUMBLE. Months of the year Word in jumbled form e r r f b u y a Word in jumbled form e r r f b u y a february Click for the answer Next Question.
DATE POWER 2 INCOME JANUARY 100member X 25.00P2, FEBRUARY 200member X 25.00P5, MARCH 400member X 25.00P10, APRIL 800member.
App Engine Web App Framework Jim Eng
>> PHP: Insert Query & Form Processing. Insert Query Step 1: Define Form Variables Step 2: Make DB Connection Step 3: Error Handling Step 4: Define the.
2011 Calendar Important Dates/Events/Homework. SunSatFriThursWedTuesMon January
July 2007 SundayMondayTuesdayWednesdayThursdayFridaySaturday
School Year Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own.
CS2021 Week 12 Web Development – Building Login System
An introduction to outlining in PowerPoint
Dictation practice 2nd Form Ms. Micaela-Ms. Verónica.
TIMELINES PHOTOS This is an example text
TIMELINES PHOTOS This is an example text
McDonald’s Kalender 2009.
McDonald’s Kalender 2009.
School Year Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation.
1   1.テキストの入れ替え テキストを自由に入れ替えることができます。 フチなし全面印刷がおすすめです。 印刷のポイント.
January MON TUE WED THU FRI SAT SUN
January MON TUE WED THU FRI SAT SUN
2017/18 Payment Calendar Due Date Cut-off Day 1st of every month
McDonald’s Kalender 2009.
January Sun Mon Tue Wed Thu Fri Sat
January MON TUE WED THU FRI SAT SUN
January MON TUE WED THU FRI SAT SUN
Problem Gambling Clicks to Opgr.org
2300 (11PM) September 21 Blue line is meridian..
January MON TUE WED THU FRI SAT SUN
McDonald’s calendar 2007.
1 - January - Sun Mon The Wed Thu Fri Sat
Proud As A Peacock! We are very proud of__________________
Teacher name August phone: Enter text here.
School Year Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation.
January MON TUE WED THU FRI SAT SUN
JANUARY 1 Sun Mon Tue Wed Thu Fri Sat
January MON TUE WED THU FRI SAT SUN
JANUARY 1 Sun Mon Tue Wed Thu Fri Sat
February 2007 Note: Source:.
January 2015 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Who Wants to be a Millionaire?
January MON TUE WED THU FRI SAT SUN
MONTHS OF THE YEAR January February April March June May July August
S M T W F S M T W F
JANUARY 1 Sun Mon Tue Wed Thu Fri Sat
McDonald’s calendar 2007.
1 January 2018 Sun Mon Tue Wed Thu Fri Sat
Production Month Sun Hours K Monthly Kwh Tou Peak Value After Kwh
E W ©
Habitat Changes and Fish Migration
January Monday Tuesday Wednesday Thursday Friday Saturday Sunday 30 31
2015 January February March April May June July August September
Habitat Changes and Fish Migration
E W ©
Web Forms.
Presentation transcript:

Python Web Development CS2021-Week 9 - Forms Python Web Development

HTML Forms https://www.udacity.com/wiki/cs253/unit-2HTML Form for Submitting input: <form> <input name="q"> <input type="submit"> </form> Web Application: import webapp2 form = """ <form action="http://www.google.com/search"> <input name="q"> <input type="submit"> </form> ""“ class MainPage(webapp2.RequestHandler): def get(self): #self.response.headers['Content-Type'] = 'text/plain' self.response.out.write(form) app = webapp2.WSGIApplication([('/', MainPage)], debug=True)

More Handlers import webapp2 form = """ <form action="/testform"> <input name="q"> <input type="submit"> </form> """ class MainPage(webapp2.RequestHandler): def get(self): #self.response.headers['Content-Type'] = 'text/plain' self.response.out.write(form) class TestHandler(webapp2.RequestHandler): q=self.request.get("q") self.response.out.write(q) app = webapp2.WSGIApplication([('/', MainPage), ('/testform', TestHandler)], debug=True) Modify your code so that: class TestHandler(webapp2.RequestHandler): def get(self): #q=self.request.get("q") #self.response.out.write(q) self.response.headers ['Content-Type'] = 'text/plain' self.response.out.write(self.request)

Validation of Input import webapp2 form = """ <form method="post"> <input name="q"><input type="submit"> </form> """ class MainPage(webapp2.RequestHandler): def get(self): self.response.out.write(form) def post(self): self.response.out.write("Thanks! Valid day!") app = webapp2.WSGIApplication([('/', MainPage)], debug=True) Replace the Form: <form method="post"> What is your birthday? <br> <label> Month <input type="text" name="month"> </label> Day <input type="text" name="day"> Year <input type="text" name="year"> <br><br> <input type="submit"> </form>

Responding Based on Validation def post(self): user_month = valid_month(self.request.get('month')) user_day = valid_day(self.request.get('day')) user_year = valid_year(self.request.get('year')) if not(user_month and user_day and user_year): self.response.out.write(form) else: self.response.out.write("Thanks! That's a totally valid day!") Exercise: Write functions valid_month(), valid_day(), valid_year(). And then run your code to test your solutions.

# print valid_month("january") # => "January" # ----------- # User Instructions # # Modify the valid_month() function to verify # whether the data a user enters is a valid # month. If the passed in parameter 'month' # is not a valid month, return None. # If 'month' is a valid month, then return # the name of the month with the first letter # capitalized. months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] def valid_month(month): Write the function so that you get the following responses (use the string function .capitalize()) : # print valid_month("january") # => "January" # print valid_month("January") # print valid_month("foo") # => None # print valid_month("")

Redirection Instead of rendering the result in a post, we send them to another page that says "Thanks!". If the user's post is successful, the server sends a redirect message that causes the browser to get the "success" page. Why is it nice to redirect after a form submission? So that reloading the page doesn't resubmit the form. So we can have distinct pages for forms and success pages. Implementing Redirection Let's make the change to our application. We need to: make a "thanks" handler. add the /thanks URL. redirect to the /thanks URL.

handler for redirects class ThanksHandler(webapp2.RequestHandler): def get(self) self.response.out.write("Thanks! That's a totally valid day!") app = webapp2.WSGIApplication([('/', MainPage), ('/thanks', ThanksHandler)], debug=True) class MainPage(webapp2.RequestHandler): def post(self): user_month = self.request.get('month') user_day = self.request.get('day') user_year = self.request.get('year') month = valid_month(user_month) day = valid_day(user_day) year = valid_year(user_year) if not(month and day and year): self.write_form("That doesn't look valid to me, friend.", user_month, user_day, user_year) else: self.redirect("/thanks")

Final Version of our Application using Validation and Redirect import webapp2 import cgi def escape_html(s): return cgi.escape(s, quote = True) form = """ <form method="post"> What is your birthday? <br> <label> Month <input type="text" name="month" value="%(month)s"> </label> Day <input type="text" name="day" value="%(day)s"> Year <input type="text" name="year" value="%(year)s"> <div style="color: red">%(error)s</div> <br><br> <input type="submit"> </form> """ months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] def valid_day(day): if(day and day.isdigit()): day = int(day) if(day < 32 and day > 0): return day def valid_month(month): if(month): month = month.capitalize() if(month in months): return month def valid_year(year): if(year and year.isdigit()): year = int(year) if(year < 2020 and year > 1880): return year class MainPage(webapp2.RequestHandler): def write_form(self, error="", month="", day="", year=""): self.response.out.write(form %{"error": error, "month": escape_html(month), "day": escape_html(day), "year": escape_html(year)}) def get(self): self.write_form() def post(self): user_month = self.request.get('month') user_day = self.request.get('day') user_year = self.request.get('year') month = valid_month(user_month) day = valid_day(user_day) year = valid_year(user_year) if not(month and day and year): self.write_form("That doesn't look valid to me, friend.", user_month, user_day, user_year) else: self.redirect("/thanks") class ThanksHandler(webapp2.RequestHandler): self.response.out.write("Thanks! That's a totally valid day!") app = webapp2.WSGIApplication([('/', MainPage), ('/thanks', ThanksHandler)], debug=True) Final Version of our Application using Validation and Redirect

Homework for Problem Set 2 https://www.udacity.com/course/viewer#!/c-cs253/l-48756009/e-48538417/m-48696518