Django:使用forms.DecimalField的max_decimal_places选项时的TypeError

时间:2015-10-11 07:20:52

标签: python django forms django-forms django-1.8

我使用Django 1.8版。我有一个非常简单的表格,我在下面显示。它只包含一个字段DecimalField。当我尝试在整个项目中渲染任何模板时,我得到了

  

TypeError at / whatever /

     

__ init __()得到了一个意外的关键字参数' max_decimal_places'

这是我完整的 forms.py 文件:

from django import forms

class BidForm(forms.Form):
    amount = forms.DecimalField(label="Bid amount", max_decimal_places=2)

如果我删除错误消息似乎抱怨的max_decimal_places=2 kwarg,我的网站呈现正常,一切正常。

为什么我似乎无法使用max_decimal_places

完成堆栈跟踪。

Environment:


Request Method: GET
Request URL: http://myproject.username.webfactional.com/myapp/

Django Version: 1.8.4
Python Version: 3.4.1
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.admin',
 'django.contrib.staticfiles',
 'localflavor',
 'myapp')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware')


Traceback:
File "/home/username/webapps/myapplication/lib/python3.4/Django-1.8.4-py3.4.egg/django/core/handlers/base.py" in get_response
  119.                 resolver_match = resolver.resolve(request.path_info)
File "/home/username/webapps/myapplication/lib/python3.4/Django-1.8.4-py3.4.egg/django/core/urlresolvers.py" in resolve
  366.             for pattern in self.url_patterns:
File "/home/username/webapps/myapplication/lib/python3.4/Django-1.8.4-py3.4.egg/django/core/urlresolvers.py" in url_patterns
  402.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/username/webapps/myapplication/lib/python3.4/Django-1.8.4-py3.4.egg/django/core/urlresolvers.py" in urlconf_module
  396.             self._urlconf_module = import_module(self.urlconf_name)
File "/usr/local/lib/python3.4/importlib/__init__.py" in import_module
  109.     return _bootstrap._gcd_import(name[level:], package, level)
File "/home/username/webapps/myapplication/myproject/myproject/urls.py" in <module>
  8.     url(r'^myapp/', include('myapp.urls', namespace='myapp')),
File "/home/username/webapps/myapplication/lib/python3.4/Django-1.8.4-py3.4.egg/django/conf/urls/__init__.py" in include
  33.         urlconf_module = import_module(urlconf_module)
File "/usr/local/lib/python3.4/importlib/__init__.py" in import_module
  109.     return _bootstrap._gcd_import(name[level:], package, level)
File "/home/username/webapps/myapplication/myproject/myapp/urls.py" in <module>
  2. from . import views
File "/home/username/webapps/myapplication/myproject/myapp/views.py" in <module>
  3. from .forms import BidForm
File "/home/username/webapps/myapplication/myproject/myapp/forms.py" in <module>
  3. class BidForm(forms.Form):
File "/home/username/webapps/myapplication/myproject/myapp/forms.py" in BidForm
  11.     amount = forms.DecimalField(label="Bid amount", max_decimal_places=2)
File "/home/username/webapps/myapplication/lib/python3.4/Django-1.8.4-py3.4.egg/django/forms/fields.py" in __init__
  334.         super(DecimalField, self).__init__(max_value, min_value, *args, **kwargs)
File "/home/username/webapps/myapplication/lib/python3.4/Django-1.8.4-py3.4.egg/django/forms/fields.py" in __init__
  245.         super(IntegerField, self).__init__(*args, **kwargs)

Exception Type: TypeError at /myapp/
Exception Value: __init__() got an unexpected keyword argument 'max_decimal_places'

1 个答案:

答案 0 :(得分:3)

请参阅有关DecimalField的docs

您可以使用decimal_places

  

允许的最大小数位数。