0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

djangoでtemplateを使用する。

0
Posted at
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()メソッドを書く。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?