类型 error:在 django 2.0 中,在 include() 的情况下,View 必须是可调用的或列表/元组



我安装了Python 3.6和Django 2.0。 我一直有同样的错误...类型错误:在 django 2.0 中,在 include(( 的情况下,View 必须是可调用的或列表/元组

my app urls应用的名称是目录

from django.urls import path
from django import views
urlpatterns = [
path('index/',
views.catalog_home, name='catalog_home'),
path('category/<category_slug>/',
views.show_category, name='show_category'),
path('product/<product_slug>/',
views.show_product, name='show_product')
]

我的项目网址这是项目网址

from django.contrib import admin
from django.urls import path, include
from django.views import static
urlpatterns = [
path('admin/', admin.site.urls),
path('catalog/', include('catalog.urls'),),
path('static/', static,
{
'document_root': 'C:/Users/USER1/PycharmProjects/untitled13/static'
}),
]

我的观点

from django.shortcuts import get_object_or_404, render_to_response
from catalog.models import Category, Product
from django.template import RequestContext

def index(request, template_name='catalog/index.html'):
page_title = 'online shop for all items'
return render_to_response(template_name, locals(),
context_instance=RequestContext(request))

def show_category(request, category_slug, template_name='catalog/category.html'):
c = get_object_or_404(Category, slug=category_slug)
products = c.product_set.all()
page_title = c.name
meta_keywords = c.meta_keywords
meta_description = c.meta_description
return render_to_response(template_name, locals(),
context_instance=RequestContext(request))

def show_product(request, product_slug, template_name='catalog/product.html'):
p = get_object_or_404(Product, slug=product_slug)
categories = p.categories.filter(is_active=True)
page_title = p.name
meta_keywords = p.meta_keywords
meta_description = p.meta_description
return render_to_response(template_name, locals(),
context_instance=RequestContext(request))

我想你错过了静态的实际参考。在urlpatterns

中将static更改为static.serve

from django.contrib import admin
from django.urls import path, include
from django.views import static
urlpatterns = [
path('admin/', admin.site.urls),
path('catalog/', include('catalog.urls'), ),
path('static/', static.serve,
{
'document_root': 'C:/Users/USER1/PycharmProjects/untitled13/static'
}),
]


参考 : 服务开发中的文件

最新更新