gpt4 book ai didi

python - 尝试使用 django rest 框架运行 django 时没有名为 http_client 的模块错误

转载 作者:太空狗 更新时间:2023-10-30 02:59:47 31 4
gpt4 key购买 nike

我正在尝试使用 django rest 框架创建一个简单的 API。在 View 中,我有以下代码。

    from django.shortcuts import render
from moviestash.models import Movie
from moviestash.serializer import MovieSerializer
from rest_framework import generics

#List all movies and add movies
class MovieList(generics.ListCreateAPIView):
queryset = Movie.objects.all()
serializer_class = MovieSerializer

#Get a movie and delete a movie
class MovieDetail(generics.RetrieveDestroyAPIView):
queryset = Movie.objects.all()
serializer_class = MovieSerializer

当我运行服务器并尝试转到任何 url 时,我收到以下错误。

Request Method: GET
Request URL: http://127.0.0.1:8000/

Django Version: 1.6
Python Version: 2.7.0
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'moviestash',
'south',
'rest_framework')
Installed Middleware:
('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 "N:\Python\venvs\rest_api\lib\site-packages\django\core\handlers\base.py" in get_response
101. resolver_match = resolver.resolve(request.path_info)
File "N:\Python\venvs\rest_api\lib\site-packages\django\core\urlresolvers.py" in resolve
318. for pattern in self.url_patterns:
File "N:\Python\venvs\rest_api\lib\site-packages\django\core\urlresolvers.py" in url_patterns
346. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "N:\Python\venvs\rest_api\lib\site-packages\django\core\urlresolvers.py" in urlconf_module
341. self._urlconf_module = import_module(self.urlconf_name)
File "N:\Python\venvs\rest_api\lib\site-packages\django\utils\importlib.py" in import_module
40. __import__(name)
File "N:\Python\movies_api\movies_api\urls.py" in <module>
10. url(r'^movies/', include('moviestash.urls')),
File "N:\Python\venvs\rest_api\lib\site-packages\django\conf\urls\__init__.py" in include
26. urlconf_module = import_module(urlconf_module)
File "N:\Python\venvs\rest_api\lib\site-packages\django\utils\importlib.py" in import_module
40. __import__(name)
File "N:\Python\movies_api\moviestash\urls.py" in <module>
3. from . import views
File "N:\Python\movies_api\moviestash\views.py" in <module>
4. from rest_framework import generics
File "N:\Python\venvs\rest_api\lib\site-packages\rest_framework\generics.py" in <module>
8. from rest_framework import views, mixins
File "N:\Python\venvs\rest_api\lib\site-packages\rest_framework\views.py" in <module>
14. from rest_framework.response import Response
File "N:\Python\venvs\rest_api\lib\site-packages\rest_framework\response.py" in <module>
8. from django.utils.six.moves.http_client import responses

Exception Type: ImportError at /
Exception Value: No module named http_client

当我进入 django shell 时,我可以毫无问题地执行以下导入 from django.utils.six.moves import http_client。此外,在我导入 http_client 之后,我还执行了一个 dir(http_client) 并且我可以看到响应对象,但是由于某种原因,当我尝试使用 from django.utils.six.moves.http_client import responses 导入时,我得到一个 ImportError: No module名为 http_client。这至少可以说是非常令人沮丧的。

最佳答案

看起来您正在点击 issue 2969 .如果您从 Django 1.6 升级到 1.6.11,它应该可以工作。但是,请注意 1.6 现已停产并且不会收到安全修复程序,因此理想情况下您应该升级到最新的受支持 Django 版本或最新的 LTS。

关于python - 尝试使用 django rest 框架运行 django 时没有名为 http_client 的模块错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30901719/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com