AttributeError: 'str' object has no attribute 'items' in ajax post

Hi guys,
I need to send a posto to django server, but qhen i do the post a get the following error:

Traceback (most recent call last):
File “C:\Users\goncalo\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\core\handlers\exception.py”, line 47, in inner
response = get_response(request)
File “C:\Users\goncalo\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\debug_toolbar\middleware.py”, line 67, in call
panel.generate_stats(request, response)
File “C:\Users\goncalo\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\debug_toolbar\panels\headers.py”, line 53, in generate_stats
self.response_headers = OrderedDict(sorted(response.items()))
AttributeError: ‘str’ object has no attribute ‘items’
Internal Server Error: /ajax/filter_by_user
Traceback (most recent call last):
File “C:\Users\goncalo\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\core\handlers\exception.py”, line 47, in inner
response = get_response(request)
File “C:\Users\goncalo\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\debug_toolbar\middleware.py”, line 67, in call
panel.generate_stats(request, response)
File “C:\Users\goncalo\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\debug_toolbar\panels\headers.py”, line 53, in generate_stats
self.response_headers = OrderedDict(sorted(response.items()))
AttributeError: ‘str’ object has no attribute ‘items’

my js fuction:
var userSelect = $("#userSelect").val();
var areaSelect2 = $("#areaSelect2").val();
var csrftoken = getCookie(“csrftoken”);

    header = {'X-CSRFToken': csrftoken,
              "Content-Type": "application/json",
              }
  
    $.ajax({
        type: 'POST',
        url: "ajax/filter_by_user",
        headers: header,
         data :{
            "filtro" : "user",
             "areas" : areaSelect2,
             "users" : userSelect,
            "startDate" : startDate,
            "endDate" : endDate
          },
        mode: 'same-origin',
    success: function (response) {
        console.log(response)
    },
 error: function (response) {
        // alert the error if any error occured
       console.log(response);
 }

})

in the view i only have

#views to Analytics
def filterByUser(request):
return “sucess”

urls.py
path(r’ajax/filter_by_user’, filterByUser, name=“filterByUser”)

1 Like

Hi @goncaloLopesMarques, when making a post, can you please format stack traces with three backticks ``` at the start and end of the block? It’s also helpful to have the code blocks wrapped in those as well with the spacing matching what your code has.

The issue here is that your view filterByUser is returning a string and not a response. You can return the plain string using a HttpResponse class via return HttpResponse('success')

What’s happening is that the debug toolbar expects that the view has returned a subclass of HttpResponse, but your view has returned a string. The toolbar then attempts to call the .items function on the response, but the string object doesn’t have that method so it throws an exception.

2 Likes

Oh God ,thank you very mutch!! Sorry i didnt know about de ‘’’ thing, Next time i will.be carefull.

it works now! but the data isnt in the request.post.get(), instead i only can acess the data in request.body, what i´m doing wrong?

You’re not doing anything wrong. The request.POST structure is used for form-submission data, not JSON. When you submit JSON, you access it in the view via request.body.

1 Like