Import Error : from django.core.handlers.wsgi impo

2019-08-24 10:58发布

问题:

I am super new with django and python and I'm having trouble getting the django rest framework integrated into my project.

I've got my model, serializer, and api in order, I think but when I attempt to run the server I get a string of errors:

(pyDev) C:\Users\pyDev\smrt>python manage.py run server Performing system checks...

    Unhandled exception in thread started by <function wrapper at 0x0000000003FFD278
    >
    Traceback (most recent call last):
...
      File "C:\Python27\Lib\site-packages\rest_framework\generics.py", line 8, in <m
    odule>
        from rest_framework import views, mixins
      File "C:\Python27\Lib\site-packages\rest_framework\views.py", line 14, in <mod
    ule>
        from rest_framework.response import Response
      File "C:\Python27\Lib\site-packages\rest_framework\response.py", line 8, in <m
    odule>
        from django.core.handlers.wsgi import STATUS_CODE_TEXT
    ImportError: cannot import name STATUS_CODE_TEXT

Anyone have an idea of how to fix this or what I am doing wrong? Thanks!

  • python: 2.7.14
  • django: 1.11.6
  • rest framework: 3.1.1
  • virtualenv: 12.1.1

wpercy correctly points out that STATUS_CODE_TEXT has been removed from django, but I am not explicitly calling it from anywhere in my code. I guess that means it's somewhere in the rest_framework.generics code?

Is it possible that I'm using the rest_framework incorrectly and need to update my approach?

EDIT: I built a brand new project from scratch following the django rest framework tutorial, and I am able to return JSON objects just fine without the errors described here...

EDIT: This specifically appears to be the result of a call to include:

rest_framework.generics import ListAPIView

EDIT: Based on wpercy's advice I looked through the traceback more carefully and realized that the reference was pointing at my root python27\Lib\site-packages\rest_framework\response.py on line 8 as indicated above, I found:

from django.core.handlers.wsgi import STATUS_CODE_TEXT

and then on line 81:

return STATUS_CODE_TEXT.get(self.status_code, '')

How can I fix this?

EDIT: I found on the link that wpercy & commenter below posted

Link to relevant ticket. Jpadilla wrote:

In summary we should probably be able to do something like in response.py:

from django.utils.six.moves.http_client import responses

...

return responses.get(self.status_code, '')

I dropped in that suggestion which is a closed item on github:

ticket for django-rest-framework Status code

and the server will now run. I'm not sure how I ended up with this older code? Perhaps I should uninstall and reinstall django-rest-framework to ensure that I have the latest, but dropping this code in did fix the issue.

EDIT: Based on an answer I received from another question: 'Options' object has no attribute 'get_all_related_objects' but I'm already using Django 1.11 it turns out that I was using an old version of the rest framework with a new version of django. See Answer marked below

回答1:

This error is because the django-rest-framework version you're using(v3.1.1) doesn't support Django version 1.11. Support for Django v1.11 was not added in django-rest-framework until version 3.7.

Upgrade django-rest-framework using pip: pip install -U djangorestframework



回答2:

django.core.handlers.wsgi.STATUS_CODE_TEXT was removed in Django 1.9.

From the Django 1.9 release notes:

django.http.responses.REASON_PHRASES and django.core.handlers.wsgi.STATUS_CODE_TEXT have been removed. Use Python’s stdlib instead: http.client.responses for Python 3 and httplib.responses for Python 2.