简体   繁体   English

Python Django Dict对象不可调用

[英]Python Django Dict object not callable

I have been trying to debug this issue, but can't seem to figure it out. 我一直在尝试调试此问题,但似乎无法解决。

When debugging I can see that all the variables are where they should be, but I can't seem to get them out. 调试时,我可以看到所有变量都在应有的位置,但似乎无法清除它们。

When running I get the error message 'dict' object is not callable 运行时,我收到错误消息'dict' object is not callable

This is the full error message from Django 这是来自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

For clarity I have added a screenshot from the debugger variables. 为了清楚起见,我从调试器变量中添加了一个屏幕截图。 在此处输入图片说明

This is the code I have been using: 这是我一直在使用的代码:

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')

To get form_base_currency working I tried these different methods: 为了使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')

None of them work. 他们都不工作。 Could someone tell me how I can solve this? 有人可以告诉我如何解决吗?

Dictionaries need square brackets 字典需要方括号

 form_counter_currency = form.cleaned_data['form_counter_currency']

although you may want to use get so you can provide a default 尽管您可能想使用get以便提供默认值

 form_counter_currency = form.cleaned_data.get('form_counter_currency', None)

import this 导入这个

 from rest_framework.response import Response

Then after write this in views.py file 然后将其写入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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM