簡體   English   中英

當運行Django服務器時,出現錯誤:“ TypeError:視圖必須是可調用的,或者對於include(),視圖必須是列表/元組。”

[英]Getting error: “TypeError: view must be a callable or a list/tuple in the case of include().” when running Django Server

每次我嘗試使用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().

這是我的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時,一切都開始發生。 關於下一步嘗試的任何想法?

您尚未將其包括在您的問題中,但是回溯表明您仍在使用字符串代替此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}),
]

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM