LoginSignup
5
3

More than 3 years have passed since last update.

【Wagtail】Wagtailプロジェクトにログインページを追加する

Posted at

はじめに

 Web開発初心者による学んだことのアウトプットです。Djangoを使用したWeb開発の勉強中です、、、

Wagtailとは

 そもそもWagtailとは何かというと、Django上に構成されたコンテンツ管理システム(CMS)です。ざっくりいうとブログなどの管理ページを簡単に作れます。

やってみる

 早速、プロジェクトの作成からログインページの作成まで順を追ってやっていきます。

Wagtailプロジェクトを始める

 PipenvおよびVirtualenvを使う前提で進めていきます。

pip install wagtail
wagtail start mysite
cd mysite
pip install -r requirements.txt
python manage.py migrate
python manage.py createsuperuser
python manage.py runserver

 これでWagtailプロジェクトの作成でき、以下の初期ページにアクセスできると思います。

wagtail.jpeg

ログインページを追加する

 まず、django-allauthをインストールし、mysite/settingsディレクトリ下のbase.pyでAllauthを有効にします。

pip install django-allauth
base.py
'django.template.context_processors.request', # 追加する

TEMPLATES = [
    {
        # ...,
        'OPTIONS': {
            'context_processors': [
                # ...
                'django.template.context_processors.request', #なければ追加する
                # ...
            ],
        },
    },
]

INSTALLED_APPS = (
    # ... other apps 
    # 無ければ以下を追加する
    'django.contrib.auth', 
    'django.contrib.messages',
    'django.contrib.sites',

    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    # ...
)

AUTHENTICATION_BACKENDS = ( 
   'django.contrib.auth.backends.ModelBackend',
   'allauth.account.auth_backends.AuthenticationBackend',
) # 追加する

# 以下を追加する
LOGIN_URL = '/login/'
LOGIN_REDIRECT_URL = '/'
ACCOUNT_AUTHENTICATION_METHOD = "username_email"
ACCOUNT_CONFIRM_EMAIL_ON_GET = True
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_EMAIL_VERIFICATION = "mandatory"
ACCOUNT_LOGIN_ON_EMAIL_CONFIRMATION = True
ACCOUNT_LOGOUT_ON_GET = True
ACCOUNT_LOGIN_ON_PASSWORD_RESET = True
ACCOUNT_LOGOUT_REDIRECT_URL = '/login/'
ACCOUNT_PRESERVE_USERNAME_CASING = False
ACCOUNT_SESSION_REMEMBER = True
ACCOUNT_SIGNUP_PASSWORD_ENTER_TWICE = False
ACCOUNT_USERNAME_BLACKLIST = ["admin", "god"]
ACCOUNT_USERNAME_MIN_LENGTH = 2

 次に、ログインページのurlパターンを設定します。

urls.py
urlpatterns = [
    # .. Existing urls
    # 追加する
    url(r'', include('allauth.urls')), 
    url(r'', include(wagtail_urls)),
]

 最後にmigrateして、http://localhost:8000/login/ からログインページにアクセスできれば完成です。

python manage.py migrate

まとめ

 django-allauthを使ってログインページを作成する流れを確認しました。base.pyurls.pyをいじるだけでログインページ自体はすぐに導入できるので、効率的なアプリケーション開発を進められます!

参考

LEARN WAGTAIL
-HOW TO INSTALL WAGTAIL WITH DOCKER
https://learnwagtail.com/tutorials/how-install-wagtail-docker/

-ADDING USER REGISTRATION/LOGIN TO YOUR WAGTAIL WEBSITE
https://learnwagtail.com/tutorials/adding-user-authentication-registration-and-login-your-wagtail-website/

5
3
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
5
3