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でプロジェクト内で共通のテンプレートを使う

0
Posted at

背景

  • Djangoでプロジェクト内に複数のアプリがある場合に、サイト全体でスタイルを統一したい。

環境

  • Python 3.7.0 (3.7.1が出ていて、3.7.2も近々リリースされるので、入れ替え予定です)
  • Django 2.1.3

やり方

  • プロジェクト直下にtemplatesフォルダを作成
  • 上記で作成したフォルダに、sitebase.htmlを作成(サイト全体でユニークな名前にする)
  • settings.pyを修正し、テンプレートを探すフォルダに上記を追加
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates')],      # ここを変更
        'APP_DIRS': True,
        'OPTIONS': {
            (略)
        },
    },
]
  • 上記のsitebase.htmlにHTMLを記述

  • アプリ側のテンプレートで、上記を呼び出す。

{% extends 'sitebase.html' %}

感想

Djangoで静的ファイルをどこに置いてsettings.pyをどう書くのがベストか、まだ悩んでいます。

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?