代码之家  ›  专栏  ›  技术社区  ›  Lynds.

获取错误:“TypeError:在include()的情况下,视图必须是可调用的或列表/元组。”运行Django服务器时

  •  0
  • Lynds.  · 技术社区  · 8 年前

    每次我尝试使用Django运行后端服务器时,我都会得到错误“TypeError:在include()的情况下,视图必须是可调用的或列表/元组。”

        Unhandled exception in thread started by <function wrapper at 0x10e662578>
        Traceback (most recent call last):
      File "/Users/lyndseearmstrong/.virtualenvs/recipe_organizer/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
        fn(*args, **kwargs)
      File "/Users/lyndseearmstrong/.virtualenvs/recipe_organizer/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 121, in inner_run
        self.check(display_num_errors=True)
      File "/Users/lyndseearmstrong/.virtualenvs/recipe_organizer/lib/python2.7/site-packages/django/core/management/base.py", line 374, in check
        include_deployment_checks=include_deployment_checks,
      File "/Users/lyndseearmstrong/.virtualenvs/recipe_organizer/lib/python2.7/site-packages/django/core/management/base.py", line 361, in _run_checks
        return checks.run_checks(**kwargs)
      File "/Users/lyndseearmstrong/.virtualenvs/recipe_organizer/lib/python2.7/site-packages/django/core/checks/registry.py", line 81, in run_checks
        new_errors = check(app_configs=app_configs)
      File "/Users/lyndseearmstrong/.virtualenvs/recipe_organizer/lib/python2.7/site-packages/django/core/checks/urls.py", line 14, in check_url_config
        return check_resolver(resolver)
      File "/Users/lyndseearmstrong/.virtualenvs/recipe_organizer/lib/python2.7/site-packages/django/core/checks/urls.py", line 24, in check_resolver
        for pattern in resolver.url_patterns:
      File "/Users/lyndseearmstrong/.virtualenvs/recipe_organizer/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
        res = instance.__dict__[self.name] = self.func(instance)
      File "/Users/lyndseearmstrong/.virtualenvs/recipe_organizer/lib/python2.7/site-packages/django/urls/resolvers.py", line 313, in url_patterns
        patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
      File "/Users/lyndseearmstrong/.virtualenvs/recipe_organizer/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
        res = instance.__dict__[self.name] = self.func(instance)
      File "/Users/lyndseearmstrong/.virtualenvs/recipe_organizer/lib/python2.7/site-packages/django/urls/resolvers.py", line 306, in urlconf_module
        return import_module(self.urlconf_name)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
        __import__(name)
      File "/Users/lyndseearmstrong/dev/recipe_organizer/backend/recipe_organizer/urls.py", line 8, in <module>
        url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
      File "/Users/lyndseearmstrong/.virtualenvs/recipe_organizer/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 85, in url
        raise TypeError('view must be a callable or a list/tuple in the case of include().')
    TypeError: view must be a callable or a list/tuple in the case of include().
    

    这是我的URL.py:

    from django.conf.urls import url
    
    from views import RecipeList, RecipeDetail, AddRecipe
    
    
    urlpatterns = [
        url(r'^$', RecipeList.as_view(), name='recipe-list'),
        url(r'^(?P<pk>\d+)/$', RecipeDetail.as_view(), name='recipe-detail'),
        url(r'^add-recipe/$', AddRecipe.as_view(), name='add-recipe'),
    ]
    

    以下是我的观点:

    from rest_framework import generics, status
    from rest_framework.generics import CreateAPIView
    
    from serializers import RecipeSerializer
    from models import Recipe
    from rest_framework.response import Response
    
    
    class RecipeList(generics.ListAPIView):
        serializer_class = RecipeSerializer
        queryset = Recipe.objects.all()
    
    
    class RecipeDetail(generics.RetrieveUpdateDestroyAPIView):
        serializer_class = RecipeSerializer
        queryset = Recipe.objects.all()
    
    
    class AddRecipe(CreateAPIView):
        def post(self, request, *args, **kwargs):
            request.data['thumbnail'] = request.data['photo']
            serializer = RecipeSerializer(data=request.data)
            if serializer.is_valid():
                serializer.save()
                return Response(serializer.data, status=status.HTTP_201_CREATED)
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
    

    1 回复  |  直到 8 年前
        1
  •  4
  •   Alasdair    8 年前

    您的问题中没有包含它,但回溯显示您仍然使用字符串而不是可调用的URL模式:

    url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
    

    您可以通过导入 serve 查看:

    from django.views.static import serve
    
    urlpatterns = [
        ...
        url(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT}),
    ]
    

    另一种方法, as recommended by the docs ,是用来 static :

    from django.conf import settings
    from django.conf.urls.static import static
    
    urlpatterns = [
        # ... the rest of your URLconf goes here ...
    ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)