Wagtailのサイトにユーザー登録/ログイン機能を追加してみます。
以前、PythonにオープンソースCMS「Wagtail」をインストールする(Windows10)という記事を公開しましたが、その続きでWagtailのサイトにユーザー登録/ログイン機能を追加してみます。
■Python
今回のPythonのバージョンは、「3.8.5」を使用しています。(Windows10)(pythonランチャーでの確認)
■仮想環境の使用
> mysite\env\Scripts\activate.bat
「Wagtail」をインストールし、サイトを生成させるために、仮想環境の作成を行っていますので、その仮想環境を使用するために、コマンドプロンプトを起動し、Pythonが実行されている作業ディレクトリ(カレントディレクトリ)に移動し、上記のコマンドを入力し、Enterキーを押します。
「mysite\env\Scripts\activate.bat」を入力し、Enterキーを押すと、「(env)」と表示されますのでこれで仮想環境を使用することができます。
■django-allauthをインストールする
設定後、django-allauthをインストールします。
pip install django-allauth
インストールするために、コマンドプロンプト上で上記のコマンドを入力し、Enterキーを押します。なお、今回はpythonランチャーで切り替えを行っていますので、「py -3.8 -m pip django-allauth」でインストールします。
■コード
Defaulting to user installation because normal site-packages is not writeable Collecting django-allauth Downloading django-allauth-0.45.0.tar.gz (581 kB) |████████████████████████████████| 581 kB 930 kB/s Requirement already satisfied: Django>=2.0 in c:\users\user_\appdata\roaming\python\python38\site-packages (from django-allauth) (3.2.7) Collecting python3-openid>=3.0.8 Downloading python3_openid-3.2.0-py3-none-any.whl (133 kB) |████████████████████████████████| 133 kB 3.3 MB/s Requirement already satisfied: requests-oauthlib>=0.3.0 in c:\users\user_\appdata\roaming\python\python38\site-packages (from django-allauth) (1.3.0) Requirement already satisfied: requests in c:\users\user_\appdata\roaming\python\python38\site-packages (from django-allauth) (2.25.1) Requirement already satisfied: pyjwt[crypto]>=1.7 in c:\users\user_\appdata\roaming\python\python38\site-packages (from django-allauth) (2.1.0) Requirement already satisfied: pytz in c:\users\user_\appdata\roaming\python\python38\site-packages (from Django>=2.0->django-allauth) (2021.1) Requirement already satisfied: asgiref<4,>=3.3.2 in c:\users\user_\appdata\roaming\python\python38\site-packages (from Django>=2.0->django-allauth) (3.4.1) Requirement already satisfied: sqlparse>=0.2.2 in c:\users\user_\appdata\roaming\python\python38\site-packages (from Django>=2.0->django-allauth) (0.4.2) Requirement already satisfied: defusedxml in c:\users\user_\appdata\roaming\python\python38\site-packages (from python3-openid>=3.0.8->django-allauth) (0.7.1) Requirement already satisfied: oauthlib>=3.0.0 in c:\users\user_\appdata\roaming\python\python38\site-packages (from requests-oauthlib>=0.3.0->django-allauth) (3.1.1) Requirement already satisfied: idna<3,>=2.5 in c:\users\user_\appdata\roaming\python\python38\site-packages (from requests->django-allauth) (2.10) Requirement already satisfied: certifi>=2017.4.17 in c:\users\user_\appdata\roaming\python\python38\site-packages (from requests->django-allauth) (2021.5.30) Requirement already satisfied: urllib3<1.27,>=1.21.1 in c:\users\user_\appdata\roaming\python\python38\site-packages (from requests->django-allauth) (1.26.5) Requirement already satisfied: chardet<5,>=3.0.2 in c:\users\user_\appdata\roaming\python\python38\site-packages (from requests->django-allauth) (4.0.0) Collecting cryptography<4.0.0,>=3.3.1; extra == "crypto" Downloading cryptography-3.4.8-cp36-abi3-win_amd64.whl (1.6 MB) |████████████████████████████████| 1.6 MB 939 kB/s Requirement already satisfied: cffi>=1.12 in c:\users\user_\appdata\roaming\python\python38\site-packages (from cryptography<4.0.0,>=3.3.1; extra == "crypto"->pyjwt[crypto]>=1.7->django-allauth) (1.14.5) Requirement already satisfied: pycparser in c:\users\user_\appdata\roaming\python\python38\site-packages (from cffi>=1.12->cryptography<4.0.0,>=3.3.1; extra == "crypto"->pyjwt[crypto]>=1.7->django-allauth) (2.20) Building wheels for collected packages: django-allauth Building wheel for django-allauth (setup.py) ... done Created wheel for django-allauth: filename=django_allauth-0.45.0-py3-none-any.whl size=914487 sha256=81a3e25291c8f12ee4311f8223f8138d408e979d1ce1d616f9cdeee4f0c670f4 Stored in directory: c:\users\user_\appdata\local\pip\cache\wheelsd\f8\c63cadecc18b2ab065df9861876bab1a955f4f59ff1c79682e Successfully built django-allauth Installing collected packages: python3-openid, django-allauth, cryptography Successfully installed cryptography-3.4.8 django-allauth-0.45.0 python3-openid-3.2.0
Enterキーを押すと、「Successfully installed」と表示されましたので、これで正常にdjango-allauthがインストールされました。
■設定ファイルを開き、コードを追加する
インストール後、今回は「mysite」という名前のサイトを生成していますので、そのフォルダ内にある「base.py」をコードエディタなどで編集し、コードを追加します。
■コード
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [ os.path.join(PROJECT_DIR, 'templates'), ], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.request', ], }, }, ]
「base.py」内の「OPTIONS」に「’django.template.context_processors.request’,」を追加します。既に追加されている場合は、追加しません。
■コード
AUTHENTICATION_BACKENDS = ( 'django.contrib.auth.backends.ModelBackend', 'allauth.account.auth_backends.AuthenticationBackend', )
さらに、「base.py」内の「AUTHENTICATION_BACKENDS」という項目を作り、「’django.contrib.auth.backends.ModelBackend’,」、「’allauth.account.auth_backends.AuthenticationBackend’,」を追加します。
■コード
INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.messages', 'django.contrib.sites', 'allauth', 'allauth.account', 'allauth.socialaccount', )
次に、「base.py」内の「INSTALLED_APPS」に、上記のコードを追加します。既に追加されている場合は、追加しません。
■コード
SITE_ID = 1
次に、「base.py」内に「SITE_ID」という項目を作り、今回生成したWagtailのサイトは、シングルサイトなので、マルチ(複数)サイトを使用していませんので、1と追加します。
■コード
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"] ACCOUNT_USERNAME_MIN_LENGTH = 2
最後に、「base.py」内に、上記のコードを追加します。ユーザー登録/ログイン機能の設定を行います。
追加後、「mysite」内のフォルダにある「urls.py」というファイルを編集し、コードを追加します。
■コード
urlpatterns = [ path(r'',include('allauth.urls')), ]
「urls.py」内の「urlpatterns」に「path(r”,include(‘allauth.urls’)),」を追加します。
■データベースの更新
(env) C:\Users\user_\mysite> python manage.py migrate
追加後、データベースを更新するために、上記のコマンドを入力し、Enterキーを押します。なお、今回はpythonランチャーで切り替えを行っていますので、「py -3.8 manage.py migrate」で更新します。
WARNINGS: account.EmailAddress: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'. HINT: Configure the DEFAULT_AUTO_FIELD setting or the AccountConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'. account.EmailConfirmation: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'. HINT: Configure the DEFAULT_AUTO_FIELD setting or the AccountConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'. socialaccount.SocialAccount: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'. HINT: Configure the DEFAULT_AUTO_FIELD setting or the SocialAccountConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'. socialaccount.SocialApp: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'. HINT: Configure the DEFAULT_AUTO_FIELD setting or the SocialAccountConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'. socialaccount.SocialToken: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'. HINT: Configure the DEFAULT_AUTO_FIELD setting or the SocialAccountConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'. Operations to perform: Apply all migrations: account, admin, auth, contenttypes, home, sessions, sites, socialaccount, taggit, wagtailadmin, wagtailcore, wagtaildocs, wagtailembeds, wagtailforms, wagtailimages, wagtailredirects, wagtailsearch, wagtailusers Running migrations: No migrations to apply.
Enterキーを押すと、データベースが更新されます。今回は「WARNINGS」という表示が出ていますが、エラーではないので、一旦無視します。
■開発サーバーを起動し確認する
更新後、開発サーバーを起動し、サイトを確認します。
(env) C:\Users\user_\mysite> python manage.py runserver
サーバーを起動するために、上記のコマンドを入力し、Enterキーを押します。今回はpythonランチャーで切り替えを行っていますので、「>py -3.8 manage.py runserver」で行う。
Watching for file changes with StatReloader Performing system checks... System check identified some issues: WARNINGS: account.EmailAddress: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'. HINT: Configure the DEFAULT_AUTO_FIELD setting or the AccountConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'. account.EmailConfirmation: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'. HINT: Configure the DEFAULT_AUTO_FIELD setting or the AccountConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'. socialaccount.SocialAccount: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'. HINT: Configure the DEFAULT_AUTO_FIELD setting or the SocialAccountConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'. socialaccount.SocialApp: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'. HINT: Configure the DEFAULT_AUTO_FIELD setting or the SocialAccountConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'. socialaccount.SocialToken: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'. HINT: Configure the DEFAULT_AUTO_FIELD setting or the SocialAccountConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'. System check identified 5 issues (0 silenced). October 01, 2021 - 09:22:34 Django version 3.2.7, using settings 'mysite.settings.dev' Starting development server at http://127.0.0.1:8000/ Quit the server with CTRL-BREAK.
Enterを押すと、サーバーが起動します。「Starting development server(開発サーバーを起動します) at http://127.0.0.1:8000/」と出力されていますので、URLの後ろにloginを付けて「http://127.0.0.1:8000/login/」とし、このURLをコピーし、Webブラウザを起動します。起動後、WebブラウザのアドレスバーにコピーしたURLを貼り付けします。
なお、サーバーを起動する際にも、「WARNINGS」という表示が出ていますが、エラーではないので、一旦無視します。
アクセスすると、英語表記で「Sing In(ログイン)」ページが表示されました。これでログイン機能の追加は完了となります。
■WARNINGS: account.EmailAddress,account.EmailConfirmation等の対処
データベースの更新を更新した際に、更新を行う場合に下記のWARNINGS(警告)が表示される場合の対処について解説します。
WARNINGS: account.EmailAddress: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'. HINT: Configure the DEFAULT_AUTO_FIELD setting or the AccountConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'. account.EmailConfirmation: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'. HINT: Configure the DEFAULT_AUTO_FIELD setting or the AccountConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'. socialaccount.SocialAccount: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'. HINT: Configure the DEFAULT_AUTO_FIELD setting or the SocialAccountConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'. socialaccount.SocialApp: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'. HINT: Configure the DEFAULT_AUTO_FIELD setting or the SocialAccountConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'. socialaccount.SocialToken: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'. HINT: Configure the DEFAULT_AUTO_FIELD setting or the SocialAccountConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'. Operations to perform: Apply all migrations: account, admin, auth, contenttypes, home, sessions, sites, socialaccount, taggit, wagtailadmin, wagtailcore, wagtaildocs, wagtailembeds, wagtailforms, wagtailimages, wagtailredirects, wagtailsearch, wagtailusers Running migrations: No migrations to apply.
今回は対処の1つ(https://stackoverflow.com/questions/67783120/warning-auto-created-primary-key-used-when-not-defining-a-primary-key-type-by)として、「mysite」という生成されたサイトのフォルダ内にある「base.py」をコードエディタなどで編集し、下記のコードを追加します。
■コード
DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'
■データベースを更新し検証
(env) C:\Users\user_\mysite> python manage.py migrate
追加後、検証を行うために、上記のコマンドを入力し、Enterキーを押します。なお、今回はpythonランチャーで切り替えを行っていますので、「py -3.8 manage.py migrate」で更新します。
Operations to perform: Apply all migrations: account, admin, auth, contenttypes, home, sessions, sites, socialaccount, taggit, wagtailadmin, wagtailcore, wagtaildocs, wagtailembeds, wagtailforms, wagtailimages, wagtailredirects, wagtailsearch, wagtailusers Running migrations: No migrations to apply.
Enterキーを押し更新すると、WARNINGS: account.EmailAddress,account.EmailConfirmation等の警告が表示されなくなりました。
■開発サーバーを起動しサイトを確認する
更新後、さらに検証で開発サーバーを起動し、サイトを確認します。
(env) C:\Users\user_\mysite> python manage.py runserver
サーバーを起動するために、上記のコマンドを入力し、Enterキーを押します。今回はpythonランチャーで切り替えを行っていますので、「>py -3.8 manage.py runserver」で行う。
Watching for file changes with StatReloader Performing system checks... System check identified no issues (0 silenced). October 01, 2021 - 09:52:26 Django version 3.2.7, using settings 'mysite.settings.dev' Starting development server at http://127.0.0.1:8000/ Quit the server with CTRL-BREAK.
Enterキーを押すと、開発サーバーが起動します。この時にも、WARNINGS: account.EmailAddress,account.EmailConfirmation等の警告が表示されなくなりました。
コメント