LoginSignup
2

More than 1 year has passed since last update.

【Django】DjangoアプリにBasic認証をかける

Last updated at Posted at 2022-05-19

はじめに

wsgi-basic-authを使ったDjangoアプリにBasic認証をかけるための方法をご紹介します。
シンプルで簡単なのでオススメです。

1. ライブラリをインストールする

$ pip install wsgi-basic-auth

2. .envファイルの設定

.envファイルに、Basic認証のIDとパスワードを追加します。

.env
WSGI_AUTH_CREDENTIALS=ID:password

3. wsgi.pyの編集

wsgi.pyに以下を追加してください。(settings.pyと同じディレクトリにあります。)

wsgi.py
from wsgi_basic_auth import BasicAuth

application = BasicAuth(application)

4. runserverして確認する

Basic認証が設定できているかを確認します。

$ python manage.py runserver

ページをリロードすると、ログインのポップアップが画面上部に現れます。
basic_authentification.png

先ほど.envファイルに設定したIDとパスワードを入力し、ログインできたら成功です!

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