An article about useful frameworks or libraries needed to create REST APIs in Python. This article uses Django, the Python web framework. I think Flask will be released separately. Also, to use each library and framework with Django, you need to install and edit settings.py.
Let's take a look! !!
Django Web framework. It has all the modules you need for your web application. For example, handling directory paths and databases effortlessly. It won't start without this.
terminal
$ pip install django
Django Rest framework A collection of modules required for the REST API. Function to convert DB data to JSON format, etc. If you mention it, it will not be sharp. This is also essential.
terminal
$ pip install djangorestframework
settings.py
settings.py
INSTALLED_APPS = [
'rest_framework',
]
Add one line to INSTALLED_APPS as above. INSTALLED_APPS You can make django aware by defining an application. Parameters used when creating a migration file.
The explanation of INSTALLED_APPS is omitted hereafter.
SimpleJwt JWT (Json Web Token) can detect data tampering. Mainly used for authentication tokens. It's a security category, so it's sober, but I think it's better to use it properly in the portfolio.
$ pip install djangorestframework-simplejwt
settings.py
settings.py
from datetime import timedelta
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.IsAuthenticated',
],
'DEFAULT_AUTHENTICATION_CLASSES':[
'rest_framework_simplejwt.authentication.JWTAuthentication',
],
}
SIMPLE_JWT = {
'AUTH_HEADER_TYPES':('JWT'),
'ACCESS_TOKEN_LIFETIME':timedelta(minutes=30),
}
REST_FRAMEWORK Make the djangorest framework recognize by defining a library for API. DEFAULT_PERMISSION_CLASSES Specify the class that determines the access permission. DEFAULT_AUTHENTICATION_CLASSES Specify the class to use for authentication. rest_framework.permissions.IsAuthenticated Request required for all requests. rest_framework_simplejwt.authentication.JWTAuthentication Define SIMPLE JWT. SIMPLE_JWT You can make settings related to SIMPLE JWT. AUTH_HEADER_TYPES You can specify a token. Specify JWT this time. ACCESS_TOKEN_LIFETIME Set the duration of the token. The timedelta object adds and subtracts the date and time.
djoser django's third party module. A module that supports authentication such as registration, login, logout, and password reset. As the name suggests, it's only for django, so it only works with django's REST API.
$ pip install djoser
settings.py
settings.py
INSTALLED_APPS = [
'djoser',
]
pillow Image processing library. Used when dealing with images. Simple processing such as resizing and rotation can be performed easily. Please note that advanced processing such as face recognition is not possible.
$ pip install pillow
CORS A library that controls access from the front side. For example, the function to limit the request source. Resource sharing between origins in Japanese.
$ pip install django-cors-headers
settings.py
settings.py
INSTALLED_APPS = [
'corsheaders',
]
MIDDLEWARE = [
#django.middleware.common.Write above Common Middleware.
'corsheaders.middleware.CorsMiddleware',
]
CORS_ORIGIN_WHITELIST=[
#Origin to allow
"http://localhost:3000"
]
MIDDLEWARE By defining a list of middleware, django will execute it sequentially after HTTP processing. Since it is ** sequential **, the order of writing is important. CORS_ORIGIN_WHITELIST By defining the origin to allow, other requests will be played.
If you use these, you can implement REST API for general purposes. If you have any other necessary or useful libraries, please let us know. In addition, the construction of these development environments assumes a virtual environment. If you are interested in how to build a virtual environment, please try building it quickly from the following article. Create a virtual environment with Anaconda and work with PyCharm. Thank you for reading until the end.
Implementing API authentication function by JWT in Django Rest Framework
Recommended Posts