简体   繁体   English

导入错误:从django.core.handlers.wsgi导入STATUS_CODE_TEXT

[英]Import Error : from django.core.handlers.wsgi import STATUS_CODE_TEXT

I am super new with django and python and I'm having trouble getting the django rest framework integrated into my project. 我是django和python的超级新手,无法将django rest框架集成到我的项目中。

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: 我认为我的模型,序列化程序和api顺序正确,但是当我尝试运行服务器时,出现了一系列错误:

(pyDev) C:\\Users\\pyDev\\smrt>python manage.py run server Performing system checks... (pyDev)C:\\ Users \\ pyDev \\ smrt> python manage.py运行服务器正在执行系统检查...

    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 的Python:2.7.14
  • django: 1.11.6 django:1.11.6
  • rest framework: 3.1.1 休息框架:3.1.1
  • virtualenv: 12.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. wpercy正确地指出STATUS_CODE_TEXT已从Django中删除,但我并未在代码中的任何位置显式调用它。 I guess that means it's somewhere in the rest_framework.generics code? 我猜这意味着它在rest_framework.generics代码中的某处?

Is it possible that I'm using the rest_framework incorrectly and need to update my approach? 我是否可能使用了不正确的rest_framework并需要更新我的方法?

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... 编辑:在django rest框架教程之后,我从头开始构建了一个全新的项目,并且我能够很好地返回JSON对象,而没有此处描述的错误...

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: 编辑:基于wpercy的建议,我更加仔细地浏览了回溯,并意识到该引用指向第8行上的根python27 \\ Lib \\ site-packages \\ rest_framework \\ response.py,如上所述,我发现:

from django.core.handlers.wsgi import STATUS_CODE_TEXT

and then on line 81: 然后在第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 编辑:我在下面的wpercy&评论者发布的链接上找到了

Link to relevant ticket . 链接到相关票证 Jpadilla wrote: 杰帕迪拉写道:

In summary we should probably be able to do something like in response.py: 总而言之,我们应该应该可以像在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: 我放弃了该建议,该建议在github上是封闭的:

ticket for django-rest-framework Status code django-rest-framework的票据状态码

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. 也许我应该卸载并重新安装django-rest-framework以确保我具有最新版本,但是删除此代码确实可以解决该问题。

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. 编辑:根据我收到的另一个问题的答案: “选项”对象没有属性“ get_all_related_objects”,但是我已经在使用Django 1.11 ,事实证明我正在使用旧版本的其余框架以及新版本的django 。 See Answer marked below 请参阅下面标记的答案

This error is because the django-rest-framework version you're using(v3.1.1) doesn't support Django version 1.11. 该错误是因为您正在使用的django-rest-framework版本(v3.1.1)不支持Django 1.11版本。 Support for Django v1.11 was not added in django-rest-framework until version 3.7 . 直到3.7版才在django-rest-framework添加了对Django v1.11的支持

Upgrade django-rest-framework using pip: pip install -U djangorestframework 使用pip升级django-rest-frameworkpip install -U djangorestframework

django.core.handlers.wsgi.STATUS_CODE_TEXT was removed in Django 1.9. django.core.handlers.wsgi.STATUS_CODE_TEXT在Django 1.9中已删除。

From the Django 1.9 release notes : Django 1.9发行说明中

django.http.responses.REASON_PHRASES and django.core.handlers.wsgi.STATUS_CODE_TEXT have been removed. django.http.responses.REASON_PHRASES和django.core.handlers.wsgi.STATUS_CODE_TEXT已删除。 Use Python's stdlib instead: http.client.responses for Python 3 and httplib.responses for Python 2. 请改用Python的stdlib:适用于Python 3的http.client.responses和适用于Python 2的httplib.responses。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM