Z
Z
zlodiak2019-06-25 15:19:07
Django
zlodiak, 2019-06-25 15:19:07

How to handle an OPTION request?

There is a django backend. The frontend is on a different domain. A cross-domain request with POST parameters comes from the front. Please help me get these parameters in the view and, for example, output to the console.
from django.shortcuts import render
from django.contrib.auth.models import User
from django.http import JsonResponse
view:

def registration(request):
    print('----', request.POST)
    print('----', request.POST.get('username'))
    username = request.POST['username']
    email = request.POST['email']
    password = request.POST['password']

    print(username, email, password)

    if _reg_data_exist(username, email, password):
        user = User.objects.create_user(username, email, password)
        user.save()

    response = JsonResponse([{"registration_successful": True}], safe=False)
    response['Access-Control-Allow-Origin'] = '*'
    response['Access-Control-Allow-Methods'] = 'POST'
    return response

def _reg_data_exist(username, email, password):
    is_exist = True
    if len(username.strip()) == 0: is_exist = False
    if len(email.strip()) == 0: is_exist = False
    if len(password.strip()) == 0: is_exist = False
    return is_exist

The problem is that cross-domain requests consist of two requests. The OPTION request is sent first, and then the main one. In my case, the view interrupts django when it receives an OPTION request:
System check identified no issues (0 silenced).
June 25, 2019 - 12:09:59
Django version 2.2, using settings 'run_res.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
---- <QueryDict: {}>
---- None
Internal Server Error: /app_auth/registration
Traceback (most recent call last):
  File "/home/kalinin/django/run_res/venv/lib/python3.6/site-packages/django/utils/datastructures.py", line 78, in __getitem__
    list_ = super().__getitem__(key)
KeyError: 'username'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/kalinin/django/run_res/venv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/kalinin/django/run_res/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/kalinin/django/run_res/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/kalinin/django/run_res/app_auth/views.py", line 9, in registration
    username = request.POST['username']
  File "/home/kalinin/django/run_res/venv/lib/python3.6/site-packages/django/utils/datastructures.py", line 80, in __getitem__
    raise MultiValueDictKeyError(key)
django.utils.datastructures.MultiValueDictKeyError: 'username'
[25/Jun/2019 12:10:04] "OPTIONS /app_auth/registration HTTP/1.1" 500 76427

From the front, the request looks like this:
5d121131b5127026047629.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alternativshik, 2019-06-25
@zlodiak

response['Access-Control-Allow-Methods'] = 'GET'
allow GET, but send a post

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question