2

当我尝试提交此 DJango 表单时,我收到一个内部服务器错误“TypeError:valid_month() 恰好需要 1 个参数(给定 2 个)”。在我看来,我只将一个参数传递给 valid_month(),而不是两个。你能帮我理解我在这里做错了什么吗?我正在使用谷歌应用引擎启动器来测试这个。

import webapp2

form="""
<form method="post">
    What is your birthday?<br>
    <label>
        <input type="text" name="month">
    </label>
    <label>
        <input type="text" name="day">
    </label>
    <label>
        <input type="text" name="year">
    </label>
    <br><br>
    <input type="submit">
</form>
"""

表格.py

class MainHandler(webapp2.RequestHandler):
    def valid_day(day):
        if day.isdigit() and int(day) in range(1, 32):
            return int(day)

    def valid_month(month):
        months = {'jan':'January', 'feb': 'February', 'mar':'March', 'apr':'April','may':'May',
                    'jun':'June', 'jul': 'July', 'aug': 'August', 'sep': 'September',
                    'oct': 'October', 'nov': 'November', 'dec': 'December'}
        m = month.lower()[:3]
        if m in months:
            return months[m]

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

    def get(self):
        self.response.headers['Content-Type'] = 'text/html'
        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_month and user_day and user_year):
            self.response.out.write(form)
        else:
            self.response.out.write("You entered a valid date")




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

提交表单时,我收到以下回溯:

> Traceback(最近一次调用最后一次):文件
> "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py",
> 第 1535 行,在 __call__ 中
> rv = self.handle_exception(request, response, e) 文件“/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py” ,
> 第 1529 行,在 __call__ 中
> rv = self.router.dispatch(request, response) 文件“/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py” ,
> 第 1278 行,在 default_dispatcher 中
> 返回 route.handler_adapter(request, response) 文件“/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py”,
> 第 1102 行,在 __call__ 中
> 返回 handler.dispatch() 文件“/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py”,
> 第 572 行,调度中
> 返回 self.handle_exception(e, self.app.debug) 文件“/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py ",
> 第 570 行,正在调度中
> 返回方法(*args, **kwargs) 文件“/Users/macuser/Documents/UdactyCS253/HelloWorld/hello/main.py”,行
> 58,在岗
> user_month = self.valid_month(self.request.get('month')) 类型错误:valid_month() 只需要 1 个参数(给定 2 个)
4

2 回答 2

1

快速而肮脏的解决方案是向,和 函数添加self参数:valid_dayvalid_monthvalid_year

class MainHandler(webapp2.RequestHandler):
    def valid_day(self, day):
        if day.isdigit() and int(day) in range(1, 32):
            return int(day)

    def valid_month(self, month):
        months = {'jan':'January', 'feb': 'February', 'mar':'March', 'apr':'April','may':'May',
                    'jun':'June', 'jul': 'July', 'aug': 'August', 'sep': 'September',
                    'oct': 'October', 'nov': 'November', 'dec': 'December'}
        m = month.lower()[:3]
        if m in months:
            return months[m]

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

    ...

但是,更好的做法是 move valid_dayvalid_month并且valid_yearwebapp2.RequestHandlerbecause 之外,您应该仅在类方法与类相关并且需要实例时才定义它们。在您的情况下,这些辅助函数只是验证日期部分 - 它们不应该被定义为webapp2.RequestHandler类上的方法。然后,在没有的情况下调用这些函数self.

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

def valid_month(month):
    months = {'jan':'January', 'feb': 'February', 'mar':'March', 'apr':'April','may':'May',
                'jun':'June', 'jul': 'July', 'aug': 'August', 'sep': 'September',
                'oct': 'October', 'nov': 'November', 'dec': 'December'}
    m = month.lower()[:3]
    if m in months:
        return months[m]

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

class MainHandler(webapp2.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/html'
        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_month and user_day and user_year):
            self.response.out.write(form)
        else:
            self.response.out.write("You entered a valid date")
于 2013-08-20T00:00:06.277 回答
0

问题 您正在发送两个请求。如您所知,您正在发送月份,但您也在发送请求。

http://www.djangobook.com/en/2.0/chapter07.html

看看那个链接。它教你所有你需要知道的关于表单和调用它们的知识。

解决方案 我建议向您的函数添加另一个参数(请求,月份)

于 2013-08-20T00:01:31.587 回答