Using Flask and Django to solve cross domain request problems
- 2021-11-02 01:17:19
- OfStack
Flask Solves Cross Domains
1. Download the flask_cors package
pip install flask-cors
2. CORS using flask_cors
Code Sample
from flask_cors import *
app = Flask(__name__)
CORS(app, supports_credentials=True)
Flask-CORS document: https://flask-cors.readthedocs.io/en/latest/
Django Solves Cross Domains
1. Install django-cors-headers
pip install django-cors-headers
2. Modify settings. py
INSTALLED_APPS = [
...
'corsheaders' ,
...
]
MIDDLEWARE_CLASSES = (
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'corsheaders.middleware.CorsMiddleware', # Add this 1 Row
'django.middleware.common.CommonMiddleware',
# 'django.middleware.csrf.CsrfViewMiddleware', Comments on this 1 Row
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
# Cross-domain increase ignorance
CORS_ALLOW_CREDENTIALS = True # Indicates whether the backend supports the cookie Operation of
CORS_ORIGIN_ALLOW_ALL = True # Allow all hosts to request your API
CORS_ORIGIN_WHITELIST = ( # White list of cross-domain requests, default to all when it is empty
'http://127.0.0.1:8080',
'http://localhost:8080',
)
CORS_ALLOW_METHODS = (
'DELETE',
'GET',
'OPTIONS',
'PATCH',
'POST',
'PUT',
'VIEW',
)
CORS_ALLOW_HEADERS = (
'XMLHttpRequest',
'X_FILENAME',
'accept-encoding',
'authorization',
'content-type',
'dnt',
'origin',
'user-agent',
'x-csrftoken',
'x-requested-with',
)
Supplement: Differences between Flask and Django
(1) Flask
Flask is really "light" and deserves to be Micro Framework. Developer 1 who switched from Django to Flask will feel this way unless both of them have been used in depth
Flask is free, flexible and expandable, and the third party library has a wide range of choices. When developing, you can combine your favorite wheels and the most popular and powerful Python library
Easy to get started, even if you don't have much experience in web development, you can make a website quickly
Very suitable for small websites
API is very suitable for developing web services
There is no pressure to develop a large website, but the code architecture needs to be designed by itself, and the development cost depends on the developer's ability and experience
All aspects of performance are equal to or better than Django
Django comes with or has received rave reviews from the third party. There will always be a third party library similar to it on Flask
Flask is developed flexibly, and Python masters basically like Flask, but Django may not be praised or criticized
The cooperation of Flask with relational database is not weaker than that of Django, while the cooperation of NoSQL with relational database is far better than that of Django
Flask is more Pythonic than Django and more consistent with philosophy of Python
(2) Django
Django is too heavy. Besides web framework, it comes with ORM and template engine, which is not flexible and free enough
Django can develop small applications, but there will always be the feeling of "killing chickens with a knife"
The self-contained ORM of Django is excellent, and its comprehensive evaluation is slightly higher than that of SQLAlchemy
The template engine of Django is simple and easy to use, but its powerful degree and comprehensive evaluation are slightly lower than those of Jinja
Django comes with ORM, which also makes Django highly coupled with relational database. If you want to use NoSQL data such as MongoDB, you need to select a suitable third-party library, and you always feel that Django+SQL is a natural pair, and Django+NoSQL cuts off half of Django
Django currently supports unofficial template engines such as Jinja
Django's own database management app received rave reviews
Django is very suitable for the development of enterprise websites: fast, reliable and stable
Django is mature, stable and perfect, but compared with Flask, the overall ecology of Django is relatively closed
Django is the forerunner of Python and web framework. There are many users, and the third library is the most abundant and the best Python library. If it can't be directly used in Django, it will be possible to find a corresponding transplant
Django is easy to get started, the development documents are detailed and perfect, and the relevant information is abundant