我一直在尝试调试此问题,但似乎无法弄明白。
调试时,我可以看到所有变量都应该是它们的位置,但我似乎无法将它们排除在外。
运行时,我收到错误消息'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')
它们都不起作用。有人能告诉我如何解决这个问题吗?
答案 0 :(得分:8)
字典需要方括号
form_counter_currency = form.cleaned_data['form_counter_currency']
虽然您可能希望使用get
,但您可以提供默认
form_counter_currency = form.cleaned_data.get('form_counter_currency', None)
答案 1 :(得分:0)
导入
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