django cross-domain problems encountered when developing requests

Using django web development will encounter a problem when the rear end everything is normal, but when the front-access back-end reports an error, the error is as follows:

Cross-domain requests error

The problem with this situation is encountered in cross-domain django of. We followed this resolve:

1. pip command to install django-cors-middleware

1 pip install django-cors-middleware

2. Use pycharm some small partners to develop, and then he made the above modules installed in the pip, and the installation was successful, but he continues to develop into pycharm time will still be an error, because the pip when not in use virtual environments , the default installation at the installation path of python. We need to adjust the path of the compiler. There are two ways to adjust.

  1) to switch to python project compiler default installation path (I was under the c drive installation path :)

  2) Installation django-cors-middleware in a virtual environment (using pycham Example :)

    This interface is not django-cors-middleware module on the right when plus it can be installed (I believe we all this ...)

 

     ps: End point module installation

3. settings.py file for the project to be modified:

 1 INSTALLED_APPS = [
 2     'django.contrib.admin',
 3     'django.contrib.auth',
 4     'django.contrib.contenttypes',
 5     'django.contrib.sessions',
 6     'django.contrib.messages',
 7     'django.contrib.staticfiles',
 8     'corsheaders',    # 添加这一行
 9     'test1',
10 ]
View Code
 1 MIDDLEWARE = [
 2     'django.middleware.security.SecurityMiddleware',
 3     'django.contrib.sessions.middleware.SessionMiddleware',
 4     'django.middleware.common.CommonMiddleware',
 5     'django.middleware.csrf.CsrfViewMiddleware',
 6     'django.contrib.auth.middleware.AuthenticationMiddleware',
 7     'django.contrib.messages.middleware.MessageMiddleware',
 8     'django.middleware.clickjacking.XFrameOptionsMiddleware ' ,
 . 9      ' corsheaders.middleware.CorsMiddleware ' ,     # add this line and the following line 
10      ' django.middleware.common.CommonMiddleware ' ,    
 . 11 ]
View Code
1 CORS_ORIGIN_ALLOW_ALL = True when after adding this line, all access will be allowed

So far, cross-domain issue has been resolved

 

Guess you like

Origin www.cnblogs.com/blackmanzhang/p/10966058.html