我的代码没有提供正确的输出

时间:2017-01-04 20:41:02

标签: python python-2.7 google-app-engine webapp2

我想显示一个表单,并在获取输入的详细信息后,如果输入的日期,月份和年份有效显示谢谢消息否则再次显示表单但问题是当我单击提交按钮时我没有得到任何输出但只有一个空白的屏幕。 Post程序中存在一些错误。

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 个答案:

答案 0 :(得分:0)

问题是您没有正确调用检查功能。最简单的解决方法就是将业务逻辑放在类之外,如:

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)

如果你打算在课堂上学习,那么你将不得不做类似

的事情
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)