Django:使用 forms.DecimalField 的 max_decimal_places 选项时出现 TypeError
Django: TypeError when using max_decimal_places option of forms.DecimalField
我使用 Django 1.8 版。我有一个非常简单的表格,如下所示。它只包含一个字段,DecimalField
。当我尝试渲染整个项目中的任何模板时,我得到
TypeError at /whatever/
__init__() got an unexpected keyword argument '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'
参见 docs 关于 DecimalField
您可以使用 decimal_places
:
The maximum number of decimal places permitted.
我使用 Django 1.8 版。我有一个非常简单的表格,如下所示。它只包含一个字段,DecimalField
。当我尝试渲染整个项目中的任何模板时,我得到
TypeError at /whatever/
__init__() got an unexpected keyword argument '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'
参见 docs 关于 DecimalField
您可以使用 decimal_places
:
The maximum number of decimal places permitted.