LoginSignup
2
0

More than 3 years have passed since last update.

Django-allauthでテンプレートを上書きするには、account配下にテンプレートを作成する

Last updated at Posted at 2020-09-21

概要

  • Django-allauthでテンプレートを上書きするには、account/login.htmlのように、account配下にテンプレートを作成します。
    • プロジェクトフォルダ内で管理できるので、venv/lib/python3.6/site-packages/allauth/account/templatesにあるファイルを書き換えずにすみ、Gitなどでバージョン管理する際に楽です。
  • 環境は、Python 3.6.9 + Django 3.1.1 + django-allauth 0.32.0 で確認しました。

内容

  • 例えばログイン画面のaccount_loginというURLは、account/login.htmlというテンプレートに対応しています。
  • この名前でテンプレートを作成すれば、デフォルトのテンプレートを上書きすることができます。
    • 公式サイトの Templates にも記載があります。
    • URLとテンプレートファイルの対応は、公式サイトの Views にあります。

具体例

  • settings.pyファイルのTEMPLATESDIRSで、テンプレートディレクトリを指定します
    • 例:manage.pyと同じ階層にtemplatesというフォルダを作ってそこに入れる場合

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR, 'templates'),  # この行を追加
        ],
        ()
    }
]
  • 上記のtemplatesフォルダ内に、account/login.htmlを作成します

$ mkdir -p templates/account/
$ echo "<h1>this is login template...</h1>" > templates/account/login.html

「account」という名前は固定

  • 「account」という名前は固定です。
    • INSTALLED_APPS にも'allauth.account',を追加していますし、
    • python3 manage.py showmigrations すると、accountというテーブルへのマイグレーションができているのがわかります。
      • 公式サイトのViewsでは、accountsaccountが混在しているのが気になります。。。(後でPR出しておきます)
2
0
2

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
2
0