views.py
class HelloWorldView(TemplateView):
template_name = 'hello.html'
template_nameでテンプレートの名前をしてする。テンプレートの場所は
settings.pyで指定する。
settings.py
BASE_DIR = Path(__file__).resolve().parent.parent
settings.pyのファイルのパスの親フォルダのさらに親フォルダ。manage.pyが入っているディレクトリ。
settings.py
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',
],
},
},
]
'DIRS': [BASE_DIR / 'templates']の部分でBASE_DIRのtemplatesフォルダを
指定している。
views.py
from django.contrib import admin
from django.urls import path
from .views import helloworldfunction, HelloWorldView
urlpatterns = [
path('admin/', admin.site.urls),
path('helloworld/', helloworldfunction),
path('helloworld2/', HelloWorldView.as_view()),
]
クラスの場合はas_view()メソッドを書く。