Home Backend Development Python Tutorial Sample code for Django cross-domain request handling

Sample code for Django cross-domain request handling

May 02, 2018 pm 02:18 PM
django code Example

This article mainly introduces the sample code about Django cross-domain request processing, which has certain reference value. Now I share it with you. Friends in need can refer to it

django handles Ajax cross-domain requests Domain access

When using javascript for ajax access, the following error occurs

Cause of error: JavaScript is for security reasons and cross-domain access is not allowed . The following figure is an explanation of cross-domain access:

Concept:

The js cross-domain mentioned here refers to the js or Python performs data transmission or communication between different domains, such as using ajax to request data from a different domain, or using js to obtain data in the framework (Django) of different domains in the page. As long as the protocol, domain name, or port are any different, they are regarded as different domains.

Solution

1. Modify the views.py file

Modify the corresponding API implementation function in views.py, Allow other domains to request data through Ajax:

todo_list = [
  {"id": "1", "content": "吃饭"},
  {"id": "2", "content": "吃饭"},
]


class Query(View):
  @staticmethod
  def get(request):
    response = JsonResponse(todo_list, safe=False)
    response["Access-Control-Allow-Origin"] = "*"
    response["Access-Control-Allow-Methods"] = "POST, GET, OPTIONS"
    response["Access-Control-Max-Age"] = "1000"
    response["Access-Control-Allow-Headers"] = "*"
    return response

  @staticmethod
  def post(request):
    print(request.POST)
    return HttpResponse()
Copy after login

2. Add middleware django-cors-headers

GitHub address: https:// github.com/ottoyiu/django-cors-headers

2.1. Install pip install django-cors-headers

2. 2 Add app

INSTALLED_APPS = (
  ...
  'corsheaders',
  ...
)
Copy after login

2.3 Add middleware

MIDDLEWARE = [ # Or MIDDLEWARE_CLASSES on Django < 1.10
  ...
  'corsheaders.middleware.CorsMiddleware',
  'django.middleware.common.CommonMiddleware',
  ...
]
Copy after login

2.4 Configure the address that allows cross-site access to this site

CORS_ORIGIN_ALLOW_ALL = False
CORS_ORIGIN_WHITELIST = (
   'localhost:63343',
)

# 默认值是全部:
CORS_ORIGIN_WHITELIST = () # 或者定义允许的匹配路径正则表达式.
CORS_ORIGIN_REGEX_WHITELIST = ('^(https?://)?(\w+.)?>google.com$', )  # 默认值:
CORS_ORIGIN_REGEX_WHITELIST = ()
Copy after login

2.5 Set the allowed access method

CORS_ALLOW_METHODS = (
'GET',
'POST',
'PUT',
'PATCH',
'DELETE',
'OPTIONS'
)
Copy after login

2.6 Set the allowed header:

Default value:

CORS_ALLOW_HEADERS = (
'x-requested-with',
'content-type',
'accept',
'origin',
'authorization',
'x-csrftoken'
)
Copy after login

Related recommendations:

Django examples of using logging to print logs

Django Project Practical User Avatar Uploading and Access

The above is the detailed content of Sample code for Django cross-domain request handling. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What to do if the blue screen code 0x0000001 occurs What to do if the blue screen code 0x0000001 occurs Feb 23, 2024 am 08:09 AM

What to do if the blue screen code 0x0000001 occurs

Resolve code 0xc000007b error Resolve code 0xc000007b error Feb 18, 2024 pm 07:34 PM

Resolve code 0xc000007b error

GE universal remote codes program on any device GE universal remote codes program on any device Mar 02, 2024 pm 01:58 PM

GE universal remote codes program on any device

What does the blue screen code 0x000000d1 represent? What does the blue screen code 0x000000d1 represent? Feb 18, 2024 pm 01:35 PM

What does the blue screen code 0x000000d1 represent?

Go language indentation specifications and examples Go language indentation specifications and examples Mar 22, 2024 pm 09:33 PM

Go language indentation specifications and examples

Tsinghua University and Zhipu AI open source GLM-4: launching a new revolution in natural language processing Tsinghua University and Zhipu AI open source GLM-4: launching a new revolution in natural language processing Jun 12, 2024 pm 08:38 PM

Tsinghua University and Zhipu AI open source GLM-4: launching a new revolution in natural language processing

Oracle DECODE function detailed explanation and usage examples Oracle DECODE function detailed explanation and usage examples Mar 08, 2024 pm 03:51 PM

Oracle DECODE function detailed explanation and usage examples

How to deal with computer blue screen code 0x000007b How to deal with computer blue screen code 0x000007b Feb 18, 2024 pm 06:28 PM

How to deal with computer blue screen code 0x000007b

See all articles