我正在尝试使用django rest框架创建一个简单的API。在视图中,我有以下代码。
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
当我运行服务器并尝试转到任何网址时,我收到以下错误。
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并且我可以执行以下导入而没有来自django.utils.six.moves导入http_client的问题。此外,我导入http_client后我也执行了一个目录(http_client),我可以看到响应对象,但出于某种原因,当我尝试从django.utils.six.moves.http_client导入响应导入时,我得到一个ImportError:没有模块名为http_client。至少可以说这是非常令人沮丧的。
答案 0 :(得分:4)
看起来你正在点击issue 2969。如果从Django 1.6升级到1.6.11,它应该可以工作。但是,请注意1.6现在已经过期并且没有收到安全修复程序,所以理想情况下你应该升级到最新支持的Django版本或最新的LTS。