簡體   English   中英

Python Django Dict對象不可調用

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM