目前我在设置中将我的主目录设置为templates文件夹,这样我就有了项目级别的模板。在那个文件夹中,我有一个home.html文件。
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR / "templates"],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
这是我的views.py文件:
from django.views.generic import TemplateView
class HomePageView(TemplateView):
template_name: "home.html"
每当我运行服务器时,我都会收到以下错误:在/TemplateResponseMixin需要"template_name"的定义或"get_template_name(("的实现
我正在关注一本关于T(Django for Beginners(的书,我不确定为什么会发生这个错误。template_name的语法是否已更改?我检查了所有的文件,我的名字似乎还可以。我还尝试输入文件的路径名。
您正在进行注释:您应该使用等号=
而不是冒号:
:
from django.views.generic import TemplateView
class HomePageView(TemplateView):
template_name='home.html'
通常模板也是";名称空间的";通过将这些放入带有应用程序名称的目录中,因此:
from django.views.generic import TemplateView
class HomePageView(TemplateView):
template_name = 'app_name/home.html'
从而将该模板放置在CCD_ 3的目录中。这样可以防止您以后在其他应用程序中与具有相同名称的模板发生冲突。
您错误地使用了视图。用=
替换:
,应该没问题。