首页 > 解决方案 > Django Python 错误:“dict”对象没有属性“status_code”

问题描述

亲爱的人们,我有一个我无法解决的问题。发帖后出现以下错误:“dict”对象没有属性“status_code”

我正在使用 django 3.1.7 和 python 3.8。

谁能帮我解决这个问题?我是这个东西的新手,似乎无法单独解决它。

.py:

from gluon import *
from .models import *
from lxml import etree as ET
import re
import requests

def update_customer(request, p_externalcode, p_addressexternalcode, p_street, p_nr, p_addition, p_zipcode, p_city, p_country, p_complexnr, p_region, p_company_code, controle, omgeving):
    url = request.session['omgeving_url']+'/mds/soapservice/outbound.asmx'
    lic = '{'+request.session['omgeving_lic']+'}'
    gebruiker = request.session['omgeving_geb']
    wachtwoord = request.session['omgeving_pas']
    omgnaam = request.session['omgeving_naam']

    headers = {'content-type':'text/xml', 'charset':'utf-8'}

    # opbouw van de SOAP-xml
    SOAP_NS = 'http://schemas.xmlsoap.org/soap/envelope/'
    PCA_NS = 'http://www.pcamobile.com/mds'
    ns_map = {'SOAP': SOAP_NS}
    pca_map = {None: PCA_NS}

    envelope = ET.Element(ET.QName(SOAP_NS, 'Envelope'), nsmap=ns_map)
    body = ET.SubElement(envelope, ET.QName(SOAP_NS, 'Body'), nsmap=ns_map)
    rqoutboundmessage = ET.SubElement(body, ET.QName(PCA_NS,'RqOutboundMessage'), nsmap=pca_map)

    header            = ET.SubElement(rqoutboundmessage, 'Header')
    version           = ET.SubElement(header, 'Version')
    companyid         = ET.SubElement(header,'CompanyID')
    messageid         = ET.SubElement(header,'MessageID')
    messagetype       = ET.SubElement(header,'MessageType')
    testmessage       = ET.SubElement(header,'TestMessage')

    rqdefinition      = ET.SubElement(rqoutboundmessage,'RqCustomerDefinition')
    nodes             = ET.SubElement(rqdefinition,'Customers')
    updated           = ET.SubElement(nodes,'Updated')
    node              = ET.SubElement(updated,'Customer')
    externalcode      = ET.SubElement(node,'ExternalCode')

    if str(p_addressexternalcode or '') != '':
        if p_addressexternalcode[:10] == 'AholdStore':
            name  = ET.SubElement(node,'Name')
            name.text  = p_addressexternalcode[-4:]
    address           = ET.SubElement(node,'Address')

    if str(p_addressexternalcode or '') != '':
        addrexternalcode  = ET.SubElement(address,'ExternalCode')
        addrexternalcode.text  = re.sub('[^ \-\.0-9@A-Z_a-z]+', '', p_addressexternalcode)
    street            = ET.SubElement(address,'Street')
    nr                = ET.SubElement(address,'Nr')    

    if str(p_addition or '') != '':
        nraddition    = ET.SubElement(address,'NrAddition')
        nraddition.text   = p_addition
    zipcode           = ET.SubElement(address,'ZipCode')
    city              = ET.SubElement(address,'City')
    country           = ET.SubElement(address,'Country')
    countrycode       = ET.SubElement(country,'ExternalCode')

    if str(p_complexnr or '') != '':
        complexname       = ET.SubElement(address,'ComplexName')
        complexname.text  = p_complexnr
    region            = ET.SubElement(node,'Region')
    regexternalcode   = ET.SubElement(region,'ExternalCode')
    companycode         = ET.SubElement(node,'CompanyID')

    #vulling van de SOAP-xml
    companyid.text    = lic
    version.text      = '1.0'
    messageid.text    = '{00009000-0000-0000-0000-000000000000}'
    messagetype.text  = 'RqCustomerDefinition'
    testmessage.text  = str(controle)
    externalcode.text = re.sub('[^.0-9@A-Z_a-z]+', '', p_externalcode)
    street.text       = p_street
    nr.text           = str(p_nr)
    zipcode.text      = p_zipcode
    city.text         = p_city
    countrycode.text  = p_country
    regexternalcode.text= p_region
    companycode.text    = p_company_code

    # maak er een tekstfile van.
    body = ET.tostring(envelope, pretty_print=True)

    response = requests.post(url,data=body,headers=headers, auth=(gebruiker, wachtwoord))
    root = ET.fromstring(response.text.encode('ascii','ignore'))

    if response.status_code == 200:
        status = 0
    else:
        status = 1

    Verwerking.objects.create(omgeving=omgnaam, Status=status, bericht=body, antwoord=ET.tostring(root, pretty_print=True).decode())

    return status

终端响应:

    [21/Mar/2021 16:25:49] "GET /woningbeheer HTTP/1.1" 200 58463
[21/Mar/2021 16:25:50] "GET /static/js/main.js HTTP/1.1" 304 0
[21/Mar/2021 16:25:50] "GET /woningbeheer HTTP/1.1" 200 58468
Internal Server Error: /woningbeheer/syncPCAind/5897
Traceback (most recent call last):
  File "C:\Python38\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Python38\lib\site-packages\django\utils\deprecation.py", line 116, in __call__
    response = self.process_response(request, response)
  File "C:\Python38\lib\site-packages\django\middleware\common.py", line 106, in process_response
    if response.status_code == 404:
AttributeError: 'dict' object has no attribute 'status_code'
[21/Mar/2021 16:25:53] "GET /woningbeheer/syncPCAind/5897 HTTP/1.1" 500 65689
Internal Server Error: /woningbeheer/syncPCAind/9640
Traceback (most recent call last):
  File "C:\Python38\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Python38\lib\site-packages\django\utils\deprecation.py", line 116, in __call__
    response = self.process_response(request, response)
  File "C:\Python38\lib\site-packages\django\middleware\common.py", line 106, in process_response
    if response.status_code == 404:
AttributeError: 'dict' object has no attribute 'status_code'
[21/Mar/2021 16:39:41] "GET /woningbeheer/syncPCAind/9640 HTTP/1.1" 500 65689

也许有用:

AttributeError at /woningbeheer/syncPCAind/82689
'dict' object has no attribute 'status_code'
Request Method: GET
Request URL:    http://127.0.0.1:8000/woningbeheer/syncPCAind/82689
Django Version: 3.1.7
Exception Type: AttributeError
Exception Value:    
'dict' object has no attribute 'status_code'
Exception Location: C:\Python38\lib\site-packages\django\middleware\common.py, line 106, in process_response
Python Executable:  C:\Python38\python.exe
Python Version: 3.8.8
Python Path:    
['C:\\Users\\wgeboers\\OneDrive - Coen Hagedoorn Bouwgroep '
 'B.V\\Django-Docker\\databeheer',
 'C:\\Python38\\python38.zip',
 'C:\\Python38\\DLLs',
 'C:\\Python38\\lib',
 'C:\\Python38',
 'C:\\Python38\\lib\\site-packages']
Server time:    Sun, 21 Mar 2021 15:44:25 +0000

提前致谢,如果需要更多信息,请告诉我。

标签: pythondjango

解决方案


推荐阅读