【问题标题】:Getting error: "TypeError: view must be a callable or a list/tuple in the case of include()." when running Django Server出现错误:“TypeError:在 include() 的情况下,视图必须是可调用的或列表/元组。”运行 Django 服务器时
【发布时间】:2017-03-29 21:19:29
【问题描述】:

每次我尝试使用 Django 运行我的后端服务器时,我都会收到错误消息“TypeError: view must be a callable or a list/tuple in the case of 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().

这是我的 urls.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)

这一切都是在我将计算机升级到 El Capitan 时开始发生的。有什么想法可以尝试下一步吗?

【问题讨论】:

标签: python django url views


【解决方案1】:

您尚未将其包含在您的问题中,但回溯显示您仍在使用字符串而不是此 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)

【讨论】:

    猜你喜欢
    • 2019-12-14
    • 2016-11-18
    • 2018-07-06
    • 2017-06-21
    • 2017-01-23
    • 2019-11-11
    • 1970-01-01
    • 2023-03-29
    • 2023-04-09
    相关资源
    最近更新 更多