analitics

Pages

Showing posts with label django-allauth. Show all posts
Showing posts with label django-allauth. Show all posts

Saturday, May 7, 2022

Python : Use django-allauth on heruko - part 001.

Today I am going to show you how to implement a google authentication using Django on the Heroku server.
The tutorial is almost complete. This introductory part is not finalized on the display side of the style and some elements ...
This python module is needed to run the web service.
pip install gunicorn
Collecting gunicorn
  Downloading gunicorn-20.1.0-py3-none-any.whl (79 kB)
     ---------------------------------------- 79.5/79.5 KB 184.8 kB/s eta 0:00:00
Requirement already satisfied: setuptools>=3.0 in c:\python311alpha\lib\site-packages (from gunicorn) (58.1.0)
Installing collected packages: gunicorn
Successfully installed gunicorn-20.1.0
Create and activate a virtual environment:
python -m venv venv
venv\Scripts\activate
The next commands will be on the (venv)
Let's install the Django
(venv) python -m pip install django
Installing collected packages: tzdata, sqlparse, asgiref, django
Successfully installed asgiref-3.5.1 django-4.0.4 sqlparse-0.4.2 tzdata-2022.1
One way is to use this command to start the project
(venv) django-admin startproject herokuweb
I used it in this way, see the dot symbol:
(venv) django-admin startproject herokuweb . 
The next command will start an application
(venv) python manage.py startapp catafest
This python module needs to use google authentification:
(venv) pip install django-allauth
...
Successfully built django-allauth cffi
Installing collected packages: certifi, urllib3, tzdata, sqlparse, pyjwt, pycpar
ser, oauthlib, idna, defusedxml, charset-normalizer, asgiref, requests, python3-
openid, Django, cffi, requests-oauthlib, cryptography, django-allauth
Successfully installed Django-4.0.4 asgiref-3.5.1 certifi-2021.10.8 cffi-1.15.0
charset-normalizer-2.0.12 cryptography-37.0.2 defusedxml-0.7.1 django-allauth-0.
50.0 idna-3.3 oauthlib-3.2.0 pycparser-2.21 pyjwt-2.3.0 python3-openid-3.2.0 req
uests-2.27.1 requests-oauthlib-1.3.1 sqlparse-0.4.2 tzdata-2022.1 urllib3-1.26.9
Let's make changes in the settings.py file
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    'catafest',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.google',
]
...
AUTHENTIFICATION_BACKENDS = [
    'django.contrib.auth.backends.ModelBackend',
    'allauth.account.auth_backends.AuthenticationBackend',
]
...
STATIC_URL = '/static/'
SITE_ID = 1
LOGIN_REDIRECT_URL = '/'
SOCIALACCOUNT_PROVIDERS = {
'google': {
    'SCOPE': [
'profile',
'email',
    ],
'AUTH_PARAMS': {
    'access_type': 'online',
}
    }
}
Use Django features ...
python manage.py makemigrations
No changes detected
...
python manage.py migrate
...
  Applying sites.0002_alter_domain_unique... OK
  Applying socialaccount.0001_initial... OK
  Applying socialaccount.0002_token_max_lengths... OK
  Applying socialaccount.0003_extra_data_default_dict... OK
Create one superuser:
python manage.py createsuperuser
Username (leave blank to use 'catafest'):
Email address: catafest@yahoo.com
Password:
Password (again):
This password is too short. It must contain at least 8 characters.
This password is too common.
Bypass password validation and create user anyway? [y/N]: y
Superuser created successfully.
Create templates, catafest folders
Add the index.html file on the catafest folder
Make changes in url.py in the heroku web folder project:
from django.contrib import admin
    from django.urls import path, include
    from django.views.generic import TemplateView
    
    urlpatterns = [
        path('', TemplateView.as_view(template_name="catafest/index.html")),
        path('admin/', admin.site.urls),
        path('accounts/', include('allauth.urls')),
    ]
In the settings.py add templates feature make this change:
import os
...
'DIRS': [os.path.join(BASE_DIR, 'templates')],
Open a google console application and set Credentials to OAuth client ID for the web.
Follow the basic steps like for any basic project
I set for my project: Authorized JavaScript origins: https://catafest.herokuapp.com
... and: Authorized redirect URIs: https://catafest.herokuapp.com/accounts/google/login/callback
Finally, you need to have these: The 'OAuth client created' with 'Your Client ID' and 'Your Client Secret' .
You can test your Django project with this command:
python manage.py runserver
Open the admin area http://127.0.0.1:8000/admin/socialaccount/socialapp/ and add a social application
These commands will log in to the Heroku browser webpage and will create the application:
heroku login
 »   Warning: heroku update available from 7.53.0 to 7.60.2.
...
heroku create catafest
 »   Warning: heroku update available from 7.53.0 to 7.60.2.
Creating ⬢ catafest... done
https://catafest.herokuapp.com/ | https://git.heroku.com/catafest.git
Then create requirements.txt and fill it with this command:
pip freeze > requirements.txt
make this change in settings.py :

...
ALLOWED_HOSTS = ['.herokuapp.com','127.0.0.1']
...
STATIC_ROOT = os.path.join(BASE_DIR,"staticfiles")
...
Upload changes with these commands:
git add .
git commit -am "add requirements.txt and changes STATIC_ROOT"
git push heroku master
Enumerating objects: 39, done.
Counting objects: 100% (39/39), done.
...
To https://git.heroku.com/catafest.git
 * [new branch]      master -> master
Open the online Heroku application, and see all errors with the command: heroku logs --tail
This error is normal because is not set in one web service:
... at=error code=H14 desc="No web processes running" method=GET path="/" host
Create Procfile file in the web001 folder and fill it with:
Add this to the file:
web: gunicorn catafest.wsgi
Add changes and make changes all with these commands:
git add .
git commit -am "add Procfile"
Push to the Django application on the Heroku server:
git push heroku master
Enumerating objects: 6, done.
Counting objects: 100% (6/6), done.
Delta compression using up to 2 threads
...
You can test the result on my heroku application , see also this google gign in link.