iT邦幫忙

2021 iThome 鐵人賽

DAY 5
0

使用allatuh管理使用者帳號的註冊跟登入登出等等
pip安裝

pip install django-allauth

到ittest/settings.py設定如下

INSTALLED_APPS = [
    # allauth
    'django.contrib.auth',
    'django.contrib.messages',
    'django.contrib.sites',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.weixin',
]
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                # Already defined Django-related contexts here
                # `allauth` needs this from django
                'django.template.context_processors.request',
            ],
        },
    },
]
AUTHENTICATION_BACKENDS = [
    # Needed to login by username in Django admin, regardless of `allauth`
    'django.contrib.auth.backends.ModelBackend',
    # `allauth` specific authentication methods, such as login by e-mail
    'allauth.account.auth_backends.AuthenticationBackend',
]
SITE_ID = 1
SOCIALACCOUNT_PROVIDERS = {
    'google': {
        # For each OAuth based provider, either add a ``SocialApp``
        # (``socialaccount`` app) containing the required client
        # credentials, or list them here:
        'APP': {
            'client_id': '123',
            'secret': '456',
            'key': ''
        }
    }
}

到ittest/urls.py修改如下

urlpatterns = [
    path('accounts/', include('allauth.urls')),
]
## this to avoid email verification and shows at console
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

瀏覽器輸入
http://127.0.0.1:8000/accounts/email/ or http://127.0.0.1:8000/accounts/signup/
想辦法到註冊頁面
如果跳轉到http://127.0.0.1:8000/accounts/profit/ 表示可能已經登入了admin或其他帳號
可以先到/email裡面的/signout先登出
註冊畫面如下
Imgur

註冊完成後驗證信的內容會被印在console上面,如下

Subject: [example.com] Please Confirm Your E-mail Address
From: webmaster@localhost
To: test_2@fox.com
Date: Thu, 26 Aug 2021 03:35:29 -0000
Message-ID: <162994892950.22456.17054158323612480632@DESKTOP-CIM6GO4>

Hello from example.com!

You're receiving this e-mail because user test_2 has given your e-mail address to register an account on example.com.

To confirm this is correct, go to http://127.0.0.1:8000/accounts/confirm-email/Mw:1mJ6Ar:_n0VKLEmcg3ryw5OIw1PEgdiPh4aYN-SrM5W_IcUhRY/

Thank you for using example.com!
example.com

上一篇
D4 Health check 安裝與使用
下一篇
D6 allauth 採坑日記 Extending & Substituting User model (1)
系列文
Python x Django 網站實作&學習記錄30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言