-4

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)
  • Try to explain what "correct" output would mean. You should also include an error message. Looking at the example code, it should simply be displaying the form you've written in. What are you expecting to happen? – Dan O'Boyle Jan 04 '17 at 20:43
  • Questions seeking debugging help (**"why isn't this code working?"**) must include the desired behavior, *a specific problem or error* and *the shortest code necessary* to reproduce it **in the question itself**. Questions without **a clear problem statement** are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – MattDMo Jan 04 '17 at 20:49
  • Here is the tutorial: http://wsgi.tutorial.codepoint.net/parsing-the-request-post – GAEfan Jan 05 '17 at 04:47

1 Answers1

0

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)
Aaron
  • 801
  • 1
  • 7
  • 12