My code isn't giving correct output

51 views Asked by At

i want to display a form and after getting the details entered, if the day, month and year entered is valid display Thanks message else display the form again but the problem is when i click submit button i don't get any output but only a blank screen. There is some error in Post procedure.

import webapp2
form="""
<form method="post">
    When is ur birthday?
    <br>
    <label> Month
        <input type="text" name="month">
    </label>
    <label> Day
        <input type="text" name="day">
    </label>
    <label> Year
        <input type="text" name="year">
    </label>
     <br>
     <br>
     <input type="submit">
</form>
"""

class MainPage(webapp2.RequestHandler):
    months = ['January',
              'February',
              'March',
              'April',
              'May',
              'June',
              'July',
              'August',
              'September',
              'October',
              'November',
              'December']
    def valid_month(month):
        month= month.capitalize()
        if month in months:
            return month
        else:
            return None

    def valid_day(day):
        if day and day.isdigit():
            if int(day) in range(1, 32):
                return int(day)
        return None

    def valid_year(year):
        if year and year.isdigit():
            if int(year) in range (1900, 2021):
                return int(year)
        return None

    def get(self):
        self.response.out.write(form)

    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_year and user_day and user_month):
            self.response.out.write(form)
        else:
            self.response.out.write("Thanks!")


app = webapp2.WSGIApplication([
    ('/', MainPage),
], debug=True)
1

There are 1 answers

0
Aaron On

The issue was that you weren't calling the checking functions correctly. The easiest fix is just to put the business logic outside the class like:

import webapp2

form="""
<form method="post">
    When is ur birthday?
    <br>
    <label> Month
        <input type="text" name="month">
    </label>
    <label> Day
        <input type="text" name="day">
    </label>
    <label> Year
        <input type="text" name="year">
    </label>
     <br>
     <br>
     <input type="submit">
</form>
"""

months = ['January',
          'February',
          'March',
          'April',
          'May',
          'June',
          'July',
          'August',
          'September',
          'October',
          'November',
          'December']

def valid_month(month):
    month= month.capitalize()
    if month in months:
        return month
    else:
        return None

def valid_day(day):
    if day and day.isdigit():
        if int(day) in range(1, 32):
            return int(day)
    return None

def valid_year(year):
    if year and year.isdigit():
        if int(year) in range (1900, 2021):
            return int(year)
    return None


class MainPage(webapp2.RequestHandler):

    def get(self):
        self.response.out.write(form)

    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_year and user_day and user_month):
            self.response.out.write(form)
        else:
            self.response.out.write("Thanks!")


app = webapp2.WSGIApplication([
    ('/', MainPage)
], debug=True)

If you're intent on having it within the class then you're going to have to do something like

import webapp2

form="""
<form method="post">
    When is ur birthday?
    <br>
    <label> Month
        <input type="text" name="month">
    </label>
    <label> Day
        <input type="text" name="day">
    </label>
    <label> Year
        <input type="text" name="year">
    </label>
     <br>
     <br>
     <input type="submit">
</form>
"""

class MainPage(webapp2.RequestHandler):

    def valid_month(self, month):
        months = ['January',
              'February',
              'March',
              'April',
              'May',
              'June',
              'July',
              'August',
              'September',
              'October',
              'November',
              'December']

        month= month.capitalize()
        if month in months:
            return month
        else:
            return None

    def valid_day(self, day):
        if day and day.isdigit():
            if int(day) in range(1, 32):
                return int(day)
        return None

    def valid_year(self, year):
        if year and year.isdigit():
            if int(year) in range (1900, 2021):
                return int(year)
        return None

    def get(self):
        self.response.out.write(form)

    def post(self):
        user_month = self.valid_month(self.request.get("month"))
        user_day = self.valid_day(self.request.get("day"))
        user_year = self.valid_year(self.request.get("year"))
        if not (user_year and user_day and user_month):
            self.response.out.write(form)
        else:
            self.response.out.write("Thanks!")


app = webapp2.WSGIApplication([
    ('/', MainPage)
], debug=True)