django allauth Getting Started and Using Detailed Explanation

  • 2021-07-06 11:30:22
  • OfStack

django-allauth is an integrated Django application for Web site authentication, user login and account management, and third-party (social) account authentication.

Now that you know and are ready to use django-allauth, this article assumes that you have mastered the basic knowledge of django (such as building Web App with django, or even a small blog site).

Installation and Basic Configuration

Installation


pip install django-allauth

Basic configuration

1. Add the following code to your project's settings. py


TEMPLATES = [
  {
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [],
    'APP_DIRS': True,
    'OPTIONS': {
      'context_processors': [
        'django.template.context_processors.request',
      ],
    },
  },
]

AUTHENTICATION_BACKENDS = (
  'django.contrib.auth.backends.ModelBackend',
  'allauth.account.auth_backends.AuthenticationBackend',
)

INSTALLED_APPS = (
  
  #  These two django Intrinsic app It is also required, but it does not need to be added repeatedly 
  'django.contrib.auth',
  'django.contrib.sites',

  'allauth',
  'allauth.account',
  'allauth.socialaccount',

  #  Below is django-allauth The social accounts supported at present, just add what you need, don't add them all :
  'allauth.socialaccount.providers.amazon', #  Amazon 
  'allauth.socialaccount.providers.angellist',
  'allauth.socialaccount.providers.asana',
  'allauth.socialaccount.providers.auth0',
  'allauth.socialaccount.providers.authentiq',
  'allauth.socialaccount.providers.baidu', #  Baidu 
  'allauth.socialaccount.providers.basecamp',
  'allauth.socialaccount.providers.bitbucket',
  'allauth.socialaccount.providers.bitbucket_oauth2',
  'allauth.socialaccount.providers.bitly',
  'allauth.socialaccount.providers.coinbase',
  'allauth.socialaccount.providers.dataporten',
  'allauth.socialaccount.providers.daum',
  'allauth.socialaccount.providers.digitalocean',
  'allauth.socialaccount.providers.discord',
  'allauth.socialaccount.providers.douban', #  Watercress 
  'allauth.socialaccount.providers.draugiem',
  'allauth.socialaccount.providers.dropbox',
  'allauth.socialaccount.providers.dwolla',
  'allauth.socialaccount.providers.edmodo',
  'allauth.socialaccount.providers.eveonline',
  'allauth.socialaccount.providers.evernote',
  'allauth.socialaccount.providers.facebook',
  'allauth.socialaccount.providers.feedly',
  'allauth.socialaccount.providers.fivehundredpx',
  'allauth.socialaccount.providers.flickr',
  'allauth.socialaccount.providers.foursquare',
  'allauth.socialaccount.providers.fxa',
  'allauth.socialaccount.providers.github', # GitHub
  'allauth.socialaccount.providers.gitlab',
  'allauth.socialaccount.providers.google',
  'allauth.socialaccount.providers.hubic',
  'allauth.socialaccount.providers.instagram',
  'allauth.socialaccount.providers.kakao',
  'allauth.socialaccount.providers.line',
  'allauth.socialaccount.providers.linkedin',
  'allauth.socialaccount.providers.linkedin_oauth2',
  'allauth.socialaccount.providers.mailru',
  'allauth.socialaccount.providers.mailchimp',
  'allauth.socialaccount.providers.meetup',
  'allauth.socialaccount.providers.naver',
  'allauth.socialaccount.providers.odnoklassniki',
  'allauth.socialaccount.providers.openid',
  'allauth.socialaccount.providers.orcid',
  'allauth.socialaccount.providers.paypal',
  'allauth.socialaccount.providers.persona',
  'allauth.socialaccount.providers.pinterest',
  'allauth.socialaccount.providers.reddit',
  'allauth.socialaccount.providers.robinhood',
  'allauth.socialaccount.providers.shopify',
  'allauth.socialaccount.providers.slack',
  'allauth.socialaccount.providers.soundcloud',
  'allauth.socialaccount.providers.spotify',
  'allauth.socialaccount.providers.stackexchange',
  'allauth.socialaccount.providers.stripe',
  'allauth.socialaccount.providers.trello',
  'allauth.socialaccount.providers.tumblr',
  'allauth.socialaccount.providers.twentythreeandme',
  'allauth.socialaccount.providers.twitch',
  'allauth.socialaccount.providers.twitter',
  'allauth.socialaccount.providers.untappd',
  'allauth.socialaccount.providers.vimeo',
  'allauth.socialaccount.providers.vk',
  'allauth.socialaccount.providers.weibo', #  Sina Weibo 
  'allauth.socialaccount.providers.weixin', #  WeChat 
  'allauth.socialaccount.providers.windowslive',
  'allauth.socialaccount.providers.xing',
  
)

SITE_ID = 1  #  Don't miss this sentence 

2. Add the following sentence to the project's urls. py (i.e. urls. py in the same folder as setting. py):


urlpatterns = [
  url(r'^accounts/', include('allauth.urls')),
]

Tips:

If you know or have used the module django. contrib. auth. urls, then after using django-allauth, you can use account_login, account_logout, account_set_password provided by allauth...... These URLs can replace the original login, logout, password_change......

3. Execute the following command in the root directory of the project (that is, the folder where manage. py is located):


python manage.py migrate

4. Restart the server.

Add a social account to log in

Background settings

Note: Because there are two environments for doing website 1: Development environment (that is, the website is developed on the local host) and production environment (that is, the website is deployed on the server), while the homepage 1 of the development environment website is generally: http://127.0.0.1: 8000, and the production environment is similar to http://www.honkerzhou.com, for simple description, so I will directly use your domain name instead of your website homepage address below, so please distinguish your own website homepage address to avoid confusion.

Enter the background management interface (your domain name/admin/), click on the site (Sites), add a site inside, write your domain name, name at will, fill it out and save it; To add a social app to the social app (Social application) under the social account (SOCIAL ACCOUNTS), you need to fill in the relevant information (take supporting GitHub social account login as an example below). Provider: Select GitHub; Name (Name): You can write casually, only you can distinguish it. It is recommended to write GitHub;; Client id and Secret key: You need to apply for it on GitHub. If not or not, please refer to Client id and Secret key for obtaining third-party applications; Key: optional; Sites: Select your domain name under Available sites, move it to the selected sites on the right, and click Save.

Obtain Client id and Secret key for third party applications

1.GitHub

After logging in to GitHub- > Enter Settings-- > Enter Developer settings-- > Enter New OAuth App under OAuth Apps-- > Then fill in the information step by step- > You can see Client id and Secret key in the next step.

Note:

Homepage URL Fill in your domain name; Authorization callback URL Fill in your domain name/accounts/github/login/callback/.

Verification and preliminary use

Important: Cancel your administrator account! ! ! Go to this URL: Your Domain/accounts/login/, and you'll see a messy and ugly page that comes with django-allauth. If you choose to log in through GitHub account at this time and the prompt of GitHub login authorization appears, it shows that you have done the right thing in front of you. Congratulations! However, you will find that you will link to this website after authorized login: your domain name /accounts/profile/, and get a 404 page. Please don't panic at this time, you only need to add this code to your settings.py file: LOGIN_REDIRECT_URL = '/' After saving, refresh the page, and you will find that you have jumped to the main page of your website; If you want to log out, go to this website: your domain name/accounts/logout/log out.

Related articles: