[PYTHON] How to authenticate with Django Part 3

Part 2

Save the authentication token in the session

Up to the last time, we have already authenticated and issued an authentication token!

All you have to do now is return the authentication token to the client!

Django settings

First, set up a module in Django to use the session

settings.py


INSTALLED_APPS = [
    'django.contrib.sessions',
]
 
MIDDLEWARE = [
    'django.contrib.sessions.middleware.SessionMiddleware',
]

Save session

All you have to do is set an authentication token for your session!

authentivateservcice.py


#Get authentication token
authenticateToken = self._authService.authenticate(user=userCresidented)

#JSON authentication token
jsonedToken = serializers.serialize("json", authenticateToken)

#Save token in session
request.session['AuthenticateToken'] = jsonedToken

The token call looks like this

sample_view_tenplate.py


    def post(self, request):
        #Register user
        token = request.session['AuthenticateToken']
        
        #Compare and authorize the hash of token variables

Only JSON format strings can be registered in the session! So you're serializing the token and turning it into a string!

reference Django Official Document How to Use Session Session serialization term https://docs.djangoproject.com/ja/3.1/topics/http/sessions/

Django Official Document Serialization of Objects https://djangoproject.jp/doc/ja/1.0/topics/serialization.html

Recommended Posts

How to authenticate with Django Part 2
How to authenticate with Django Part 3
How to get started with Django
Authenticate Google with Django
How to develop a cart app with Django
How to implement "named_scope" of RubyOnRails with Django
How to measure execution time with Python Part 1
How to update with SQLAlchemy?
How to cast with Theano
How to separate strings with','
How to RDP with Fedora31
How to Delete with SQLAlchemy?
How to upload with Heroku, Flask, Python, Git (Part 3)
How to make a shooting game with toio (Part 1)
How to upload with Heroku, Flask, Python, Git (Part 1)
How to upload with Heroku, Flask, Python, Git (Part 2)
Test Driven Development with Django Part 3
How to cancel RT with tweepy
Steps to develop Django with VSCode
Test Driven Development with Django Part 4
Python: How to use async with
Test Driven Development with Django Part 6
How to handle static files when deploying to production with Django
How to check ORM behavior in one file with django
How to use virtualenv with PowerShell
How to deal with imbalanced data
How to install python-pip with ubuntu20.04LTS
Common html to rent with Django
How to deal with imbalanced data
[Django] How to give input values in advance with ModelForm
How to resolve CSRF Protection when using AngularJS with Django
How to get started with Python
How to reflect CSS in Django
How to deal with DistributionNotFound errors
How to Data Augmentation with PyTorch
How to calculate date with python
How to write Django1.9 environment-independent wsgi.py
How to install mysql-connector with pip3
How to INNER JOIN with SQLAlchemy
Test Driven Development with Django Part 1
How to install Anaconda with pyenv
Test Driven Development with Django Part 5
How to deal with "You have multiple authentication backends configured ..." (Django)
Here's a brief summary of how to get started with Django
How to utilize Python with Jw_cad (Part 1 What is external transformation)
[Tips] How to do template extends when creating HTML with django
[Blender] How to set shape_key with script
How to title multiple figures with matplotlib
How to use cybozu.com developer network (Part 2)
How to get parent id with sqlalchemy
How to add a package with PyCharm
How to check the version of Django
How to delete expired sessions in Django
How to install DLIB with 2020 / CUDA enabled
How to use ManyToManyField with Django's Admin
How to use OpenVPN with Ubuntu 18.04.3 LTS
How to use Cmder with PyCharm (Windows)
How to use Tweepy ~ Part 1 ~ [Getting Tweet]
How to prevent package updates with apt
How to work with BigQuery in Python
How to use Ass / Alembic with HtoA