简体   繁体   中英

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

This is the full error message from 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.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

 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

class userlist(APIView):
def get(self,request):
    user1=webdata.objects.all()
    serializer=webdataserializers(user1,many=True)
    return Response(serializer.data)
def post(self):
    pass

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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