Django内部服务器错误(只接受一个参数(给定两个))

时间:2021-05-22 19:20:14

I receive an internal server error "TypeError: valid_month() takes exactly 1 argument (2 given)" when I try and submit this DJango form. It looks to me like I'm only passing one argument to valid_month(), not two. Will you help me understand what I'm doing wrong here? I'm using the google app engine launcher to test this.

当我尝试提交这个DJango表单时,我收到一个内部服务器错误“TypeError: valid_month()恰好接受一个参数(给定两个)”。在我看来,我只向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

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)

I receive the following traceback when submitting the form:

递交表格时,我会收到以下回覆:

> 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 个解决方案

#1


1  

Quick and dirty solution will be to add self argument to valid_day, valid_month and valid_year functions:

快速而肮脏的解决方案是向valid_day、valid_month和valid_year函数添加self参数:

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

    ...

But, even better would be to move valid_day, valid_month and valid_year outside of webapp2.RequestHandler because, you should define class methods only if they are relevant to the class and need an instance. In your case, these helper functions are just validating date parts - they should not be defined as methods on webapp2.RequestHandler class. Then, call these functions without self.:

但是,更好的方法是在webapp2之外移动valid_day、valid_month和valid_year。RequestHandler因为,您应该只定义类方法,如果它们与类相关,并且需要一个实例。在您的示例中,这些helper函数只是验证日期部分——它们不应该被定义为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")

#2


0  

The Problem You are sending two requests. As you know you are sending the month, but you are also sending a request.

您发送两个请求的问题。正如您所知道的,您正在发送一个月,但是您也正在发送一个请求。

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

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

Take a look at that link. It teaches you everything you need to know about forms and calling them.

看看这个链接。它教会你所有你需要知道的关于表单的知识,并称呼它们。

Solution I would recommend adding another parameter to your function (requests, month)

我建议在您的函数中添加另一个参数(请求,月)

#1


1  

Quick and dirty solution will be to add self argument to valid_day, valid_month and valid_year functions:

快速而肮脏的解决方案是向valid_day、valid_month和valid_year函数添加self参数:

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

    ...

But, even better would be to move valid_day, valid_month and valid_year outside of webapp2.RequestHandler because, you should define class methods only if they are relevant to the class and need an instance. In your case, these helper functions are just validating date parts - they should not be defined as methods on webapp2.RequestHandler class. Then, call these functions without self.:

但是,更好的方法是在webapp2之外移动valid_day、valid_month和valid_year。RequestHandler因为,您应该只定义类方法,如果它们与类相关,并且需要一个实例。在您的示例中,这些helper函数只是验证日期部分——它们不应该被定义为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")

#2


0  

The Problem You are sending two requests. As you know you are sending the month, but you are also sending a request.

您发送两个请求的问题。正如您所知道的,您正在发送一个月,但是您也正在发送一个请求。

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

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

Take a look at that link. It teaches you everything you need to know about forms and calling them.

看看这个链接。它教会你所有你需要知道的关于表单的知识,并称呼它们。

Solution I would recommend adding another parameter to your function (requests, month)

我建议在您的函数中添加另一个参数(请求,月)