Django内部服务器错误(正好1个参数(给定2个))

时间:2013-08-19 23:57:21

标签: django django-forms runtime-error

当我尝试提交此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>
"""

forms.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 (most recent call last):   File
> "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py",
> line 1535, in __call__
>     rv = self.handle_exception(request, response, e)   File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py",
> line 1529, in __call__
>     rv = self.router.dispatch(request, response)   File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py",
> line 1278, in default_dispatcher
>     return route.handler_adapter(request, response)   File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py",
> line 1102, in __call__
>     return handler.dispatch()   File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py",
> line 572, in dispatch
>     return self.handle_exception(e, self.app.debug)   File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py",
> line 570, in dispatch
>     return method(*args, **kwargs)   File "/Users/macuser/Documents/UdactyCS253/HelloWorld/hello/main.py", line
> 58, in post
>     user_month = self.valid_month(self.request.get('month')) TypeError: valid_month() takes exactly 1 argument (2 given)

2 个答案:

答案 0 :(得分: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

    ...

但是,更好的方法是将valid_dayvalid_monthvalid_year移到webapp2.RequestHandler之外,因为只有在与类相关时才应定义类方法并需要一个实例。在您的情况下,这些辅助函数只是验证日期部分 - 它们不应该被定义为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")

答案 1 :(得分:0)

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

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

看看那个链接。它教你了解表格和调用表格所需的一切。

<强>解决方案 我建议在函数中添加另一个参数(requests,month)