[英]Python Django Dict object not callable
我一直在尝试调试此问题,但似乎无法解决。
调试时,我可以看到所有变量都在应有的位置,但似乎无法清除它们。
运行时,我收到错误消息'dict' object is not callable
这是来自Django的完整错误消息
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/?form_base_currency=7&form_counter_currency=14&form_base_amount=127
Django Version: 1.8.6
Python Version: 3.4.3
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'client']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "/home/johan/sdp/currency-converter/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/johan/sdp/currency-converter/currency_converter/client/views.py" in index
22. form_base_currency = form.cleaned_data('form_base_currency').currency_code
Exception Type: TypeError at /
Exception Value: 'dict' object is not callable
这是我一直在使用的代码:
if request.method == 'GET':
form = CurrencyConverterForm(request.GET)
if form.is_valid():
form_base_currency = form.cleaned_data('form_base_currency').currency_code
form_counter_currency = form.cleaned_data('form_counter_currency')
form_base_amount = form.data.cleaned_data('form_base_amount')
为了使form_base_currency工作,我尝试了以下不同方法:
form_base_currency = form.cleaned_data('form_base_currency').currency_code
form_base_currency = form.cleaned_data.form_base_currency.currency_code
form_base_currency = form.cleaned_data('form_base_currency.currency_code')
他们都不工作。 有人可以告诉我如何解决吗?
字典需要方括号
form_counter_currency = form.cleaned_data['form_counter_currency']
尽管您可能想使用get
以便提供默认值
form_counter_currency = form.cleaned_data.get('form_counter_currency', None)
导入这个
from rest_framework.response import Response
然后将其写入views.py文件中
class userlist(APIView):
def get(self,request):
user1=webdata.objects.all()
serializer=webdataserializers(user1,many=True)
return Response(serializer.data)
def post(self):
pass
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.