【问题标题】:Django Internal Server Error (takes exactly 1 argument (2 given))Django 内部服务器错误(只需要 1 个参数(给定 2 个))
【发布时间】:2013-08-19 23:57:21
【问题描述】:

当我尝试提交此 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(最近一次调用最后一次):文件
> "/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')) TypeError: valid_month() 只需要 1 个参数(给定 2 个)

【问题讨论】:

    标签: django django-forms runtime-error


    【解决方案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")
    

    【讨论】:

      【解决方案2】:

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

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

      看看那个链接。它会教您有关表单和调用表单的所有知识。

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

      【讨论】:

      • 谢谢。我不知道这个资源。
      猜你喜欢
      • 2010-11-11
      • 2014-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多