【问题标题】:TypeError: string indices must be integers in djangoTypeError:字符串索引必须是 Django 中的整数
【发布时间】:2019-09-08 09:34:42
【问题描述】:

我正在尝试将 json 存储到 bigchainDB 中。但问题是当我对 json 对象进行硬编码并在其他节点之间广播时,它会成功。但是当我从邮递员那里发送相同的 json 对象时,我收到了一个错误 string indices must be integers

这是我的功能

 def index(request):
    root = settings.BIGCHAINDB
    bdb = BigchainDB(root)
    alice, bob = generate_keypair(), generate_keypair()
    insertDB =  json.dumps(request.body.decode("utf-8"))
    jsonDict =  json.loads(insertDB)
    prepared_token_tx = bdb.transactions.prepare(
        operation='CREATE',
        signers=alice.public_key,
        recipients=[([bob.public_key], 10)],
        asset=jsonDict)
    fulfilled_token_tx = bdb.transactions.fulfill(
        prepared_token_tx,
        private_keys=alice.private_key)
    bdb.transactions.send_commit(fulfilled_token_tx)
    txid = fulfilled_token_tx['id']
    return HttpResponse(txid)

JSON 对象:

{“数据”:{ "cphNumber": "321", “农场名称”:“313”, "addressLine1": "13", "addressLine2": "13", “地区”:“13”, "邮政编码": "13", "corrName": "13", “corrAddressLine1”:“131”, "corrAddressLine2": "31", "corrRegion": "31", "corrCountry": "321", "corrPostal": "31", "corrMobile": "321", "corrEmail": "31", “代理名称”:“31”, "agentAddressLine1": "313", "agentAddressLine2": "132", "agentRegion": "13", "agentCountry": "132", "agentPostal": "132", “brn”:“13”, “动物物种”:“132” } }

Django Version: 2.1.7
Python Version: 3.7.1
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'api']
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/user/miniconda3/lib/python3.7/site-packages/django/core/handlers/exception.py" in inner
  34.             response = get_response(request)

File "/home/user/miniconda3/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
  126.                 response = self.process_exception_by_middleware(e, request)

File "/home/user/miniconda3/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
  124.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/user/miniconda3/lib/python3.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
  54.         return view_func(*args, **kwargs)

File "/home/user/Desktop/blockchain/api/views.py" in index
  48. 		asset=jsonDict)

File "/home/user/miniconda3/lib/python3.7/site-packages/bigchaindb_driver/driver.py" in prepare
  254.             inputs=inputs,

File "/home/user/miniconda3/lib/python3.7/site-packages/bigchaindb_driver/offchain.py" in prepare_transaction
  132.         inputs=inputs,

File "/home/user/miniconda3/lib/python3.7/functools.py" in wrapper
  824.         return dispatch(args[0].__class__)(*args, **kw)

File "/home/user/miniconda3/lib/python3.7/site-packages/bigchaindb_driver/offchain.py" in _prepare_create_transaction_dispatcher
  45.     return prepare_create_transaction(**kwargs)

File "/home/user/miniconda3/lib/python3.7/site-packages/bigchaindb_driver/offchain.py" in prepare_create_transaction
  196.         asset=asset['data'] if asset else None,

Exception Type: TypeError at /api/
Exception Value: string indices must be integers

欢迎提出任何建议。提前致谢。

【问题讨论】:

  • 请将完整的错误回溯添加到您的问题并修复代码缩进。
  • @KlausD。它与我发送数据的 POST 请求有关,因为当代替 jsonDict 时,我直接编写对象它的工作正常。在我的情况下jsonDict对象
  • 追溯在哪里?这个dumps / loads 是关于什么的?
  • @KlausD。问题已更新
  • 我想我收到了这个错误,因为当我通过 Postman 传递数据时,我是作为 json 对象传递的

标签: python json django python-3.x


【解决方案1】:

这里的问题是您在request.body 上使用json.dumps(),这已经是一个字符串。因此,当您对此使用json.loads() 时,您的数据不会被解析为dict

 def index(request):
    root = settings.BIGCHAINDB
    bdb = BigchainDB(root)
    alice, bob = generate_keypair(), generate_keypair()
    insertDB =  request.body.decode("utf-8")  # Don't use json.dumps() here
    jsonDict =  json.loads(insertDB)
    prepared_token_tx = bdb.transactions.prepare(
        operation='CREATE',
        signers=alice.public_key,
        recipients=[([bob.public_key], 10)],
        asset=jsonDict)
    fulfilled_token_tx = bdb.transactions.fulfill(
        prepared_token_tx,
        private_keys=alice.private_key)
    bdb.transactions.send_commit(fulfilled_token_tx)
    txid = fulfilled_token_tx['id']
    return HttpResponse(txid)

docs

【讨论】:

    猜你喜欢
    • 2022-07-25
    • 1970-01-01
    • 2021-03-28
    • 2021-04-28
    • 2020-06-24
    • 2016-03-26
    • 2017-09-07
    • 2018-08-19
    相关资源
    最近更新 更多