Django ошибка 400

exceptions.py

Exceptions… allow error handling to be organized cleanly in a central or high-level place within the program structure.

— Doug Hellmann, Python Exception Handling Techniques

Exception handling in REST framework views

REST framework’s views handle various exceptions, and deal with returning appropriate error responses.

The handled exceptions are:

  • Subclasses of APIException raised inside REST framework.
  • Django’s Http404 exception.
  • Django’s PermissionDenied exception.

In each case, REST framework will return a response with an appropriate status code and content-type. The body of the response will include any additional details regarding the nature of the error.

Most error responses will include a key detail in the body of the response.

For example, the following request:

DELETE http://api.example.com/foo/bar HTTP/1.1
Accept: application/json

Might receive an error response indicating that the DELETE method is not allowed on that resource:

HTTP/1.1 405 Method Not Allowed
Content-Type: application/json
Content-Length: 42

{"detail": "Method 'DELETE' not allowed."}

Validation errors are handled slightly differently, and will include the field names as the keys in the response. If the validation error was not specific to a particular field then it will use the «non_field_errors» key, or whatever string value has been set for the NON_FIELD_ERRORS_KEY setting.

An example validation error might look like this:

HTTP/1.1 400 Bad Request
Content-Type: application/json
Content-Length: 94

{"amount": ["A valid integer is required."], "description": ["This field may not be blank."]}

Custom exception handling

You can implement custom exception handling by creating a handler function that converts exceptions raised in your API views into response objects. This allows you to control the style of error responses used by your API.

The function must take a pair of arguments, the first is the exception to be handled, and the second is a dictionary containing any extra context such as the view currently being handled. The exception handler function should either return a Response object, or return None if the exception cannot be handled. If the handler returns None then the exception will be re-raised and Django will return a standard HTTP 500 ‘server error’ response.

For example, you might want to ensure that all error responses include the HTTP status code in the body of the response, like so:

HTTP/1.1 405 Method Not Allowed
Content-Type: application/json
Content-Length: 62

{"status_code": 405, "detail": "Method 'DELETE' not allowed."}

In order to alter the style of the response, you could write the following custom exception handler:

from rest_framework.views import exception_handler

def custom_exception_handler(exc, context):
    # Call REST framework's default exception handler first,
    # to get the standard error response.
    response = exception_handler(exc, context)

    # Now add the HTTP status code to the response.
    if response is not None:
        response.data['status_code'] = response.status_code

    return response

The context argument is not used by the default handler, but can be useful if the exception handler needs further information such as the view currently being handled, which can be accessed as context['view'].

The exception handler must also be configured in your settings, using the EXCEPTION_HANDLER setting key. For example:

REST_FRAMEWORK = {
    'EXCEPTION_HANDLER': 'my_project.my_app.utils.custom_exception_handler'
}

If not specified, the 'EXCEPTION_HANDLER' setting defaults to the standard exception handler provided by REST framework:

REST_FRAMEWORK = {
    'EXCEPTION_HANDLER': 'rest_framework.views.exception_handler'
}

Note that the exception handler will only be called for responses generated by raised exceptions. It will not be used for any responses returned directly by the view, such as the HTTP_400_BAD_REQUEST responses that are returned by the generic views when serializer validation fails.


API Reference

APIException

Signature: APIException()

The base class for all exceptions raised inside an APIView class or @api_view.

To provide a custom exception, subclass APIException and set the .status_code, .default_detail, and default_code attributes on the class.

For example, if your API relies on a third party service that may sometimes be unreachable, you might want to implement an exception for the «503 Service Unavailable» HTTP response code. You could do this like so:

from rest_framework.exceptions import APIException

class ServiceUnavailable(APIException):
    status_code = 503
    default_detail = 'Service temporarily unavailable, try again later.'
    default_code = 'service_unavailable'

Inspecting API exceptions

There are a number of different properties available for inspecting the status
of an API exception. You can use these to build custom exception handling
for your project.

The available attributes and methods are:

  • .detail — Return the textual description of the error.
  • .get_codes() — Return the code identifier of the error.
  • .get_full_details() — Return both the textual description and the code identifier.

In most cases the error detail will be a simple item:

>>> print(exc.detail)
You do not have permission to perform this action.
>>> print(exc.get_codes())
permission_denied
>>> print(exc.get_full_details())
{'message':'You do not have permission to perform this action.','code':'permission_denied'}

In the case of validation errors the error detail will be either a list or
dictionary of items:

>>> print(exc.detail)
{"name":"This field is required.","age":"A valid integer is required."}
>>> print(exc.get_codes())
{"name":"required","age":"invalid"}
>>> print(exc.get_full_details())
{"name":{"message":"This field is required.","code":"required"},"age":{"message":"A valid integer is required.","code":"invalid"}}

ParseError

Signature: ParseError(detail=None, code=None)

Raised if the request contains malformed data when accessing request.data.

By default this exception results in a response with the HTTP status code «400 Bad Request».

AuthenticationFailed

Signature: AuthenticationFailed(detail=None, code=None)

Raised when an incoming request includes incorrect authentication.

By default this exception results in a response with the HTTP status code «401 Unauthenticated», but it may also result in a «403 Forbidden» response, depending on the authentication scheme in use. See the authentication documentation for more details.

NotAuthenticated

Signature: NotAuthenticated(detail=None, code=None)

Raised when an unauthenticated request fails the permission checks.

By default this exception results in a response with the HTTP status code «401 Unauthenticated», but it may also result in a «403 Forbidden» response, depending on the authentication scheme in use. See the authentication documentation for more details.

PermissionDenied

Signature: PermissionDenied(detail=None, code=None)

Raised when an authenticated request fails the permission checks.

By default this exception results in a response with the HTTP status code «403 Forbidden».

NotFound

Signature: NotFound(detail=None, code=None)

Raised when a resource does not exists at the given URL. This exception is equivalent to the standard Http404 Django exception.

By default this exception results in a response with the HTTP status code «404 Not Found».

MethodNotAllowed

Signature: MethodNotAllowed(method, detail=None, code=None)

Raised when an incoming request occurs that does not map to a handler method on the view.

By default this exception results in a response with the HTTP status code «405 Method Not Allowed».

NotAcceptable

Signature: NotAcceptable(detail=None, code=None)

Raised when an incoming request occurs with an Accept header that cannot be satisfied by any of the available renderers.

By default this exception results in a response with the HTTP status code «406 Not Acceptable».

Signature: UnsupportedMediaType(media_type, detail=None, code=None)

Raised if there are no parsers that can handle the content type of the request data when accessing request.data.

By default this exception results in a response with the HTTP status code «415 Unsupported Media Type».

Throttled

Signature: Throttled(wait=None, detail=None, code=None)

Raised when an incoming request fails the throttling checks.

By default this exception results in a response with the HTTP status code «429 Too Many Requests».

ValidationError

Signature: ValidationError(detail, code=None)

The ValidationError exception is slightly different from the other APIException classes:

  • The detail argument is mandatory, not optional.
  • The detail argument may be a list or dictionary of error details, and may also be a nested data structure. By using a dictionary, you can specify field-level errors while performing object-level validation in the validate() method of a serializer. For example. raise serializers.ValidationError({'name': 'Please enter a valid name.'})
  • By convention you should import the serializers module and use a fully qualified ValidationError style, in order to differentiate it from Django’s built-in validation error. For example. raise serializers.ValidationError('This field must be an integer value.')

The ValidationError class should be used for serializer and field validation, and by validator classes. It is also raised when calling serializer.is_valid with the raise_exception keyword argument:

serializer.is_valid(raise_exception=True)

The generic views use the raise_exception=True flag, which means that you can override the style of validation error responses globally in your API. To do so, use a custom exception handler, as described above.

By default this exception results in a response with the HTTP status code «400 Bad Request».


Generic Error Views

Django REST Framework provides two error views suitable for providing generic JSON 500 Server Error and
400 Bad Request responses. (Django’s default error views provide HTML responses, which may not be appropriate for an
API-only application.)

Use these as per Django’s Customizing error views documentation.

rest_framework.exceptions.server_error

Returns a response with status code 500 and application/json content type.

Set as handler500:

handler500 = 'rest_framework.exceptions.server_error'

rest_framework.exceptions.bad_request

Returns a response with status code 400 and application/json content type.

Set as handler400:

handler400 = 'rest_framework.exceptions.bad_request'

Third party packages

The following third-party packages are also available.

DRF Standardized Errors

The drf-standardized-errors package provides an exception handler that generates the same format for all 4xx and 5xx responses. It is a drop-in replacement for the default exception handler and allows customizing the error response format without rewriting the whole exception handler. The standardized error response format is easier to document and easier to handle by API consumers.

I am new to django-1.6. When I run the django server with DEBUG = True, it’s running perfectly. But when I change DEBUG to False in the settings file, then the server stopped and it gives the following error on the command prompt:

CommandError: You must set settings.ALLOWED_HOSTS if DEBUG is False.

After I changed ALLOWED_HOSTS to ["http://127.0.0.1:8000",], in the browser I get the error:

Bad Request (400)

Is it possible to run Django without debug mode?

Xavier Holt's user avatar

Xavier Holt

14.5k4 gold badges43 silver badges56 bronze badges

asked Nov 9, 2013 at 12:11

codeimplementer's user avatar

codeimplementercodeimplementer

3,2932 gold badges14 silver badges16 bronze badges

1

The ALLOWED_HOSTS list should contain fully qualified host names, not urls. Leave out the port and the protocol. If you are using 127.0.0.1, I would add localhost to the list too:

ALLOWED_HOSTS = ['127.0.0.1', 'localhost']

You could also use * to match any host:

ALLOWED_HOSTS = ['*']

Quoting the documentation:

Values in this list can be fully qualified names (e.g. 'www.example.com'), in which case they will be matched against the request’s Host header exactly (case-insensitive, not including port). A value beginning with a period can be used as a subdomain wildcard: '.example.com' will match example.com, www.example.com, and any other subdomain of example.com. A value of '*' will match anything; in this case you are responsible to provide your own validation of the Host header (perhaps in a middleware; if so this middleware must be listed first in MIDDLEWARE_CLASSES).

Bold emphasis mine.

The status 400 response you get is due to a SuspiciousOperation exception being raised when your host header doesn’t match any values in that list.

answered Nov 9, 2013 at 12:14

Martijn Pieters's user avatar

Martijn PietersMartijn Pieters

1.0m295 gold badges4025 silver badges3320 bronze badges

5

I had the same problem and none of the answers resolved my problem. For resolving situations like this, it’s best to enable logging by adding the following config to settings.py temporarily.

LOGGING = {
   'version': 1,
   'disable_existing_loggers': False,
   'handlers': {
      'file': {
         'level': 'DEBUG',
         'class': 'logging.FileHandler',
         'filename': '/tmp/debug.log',
      },
   },
   'loggers': {
      'django': {
         'handlers': ['file'],
         'level': 'DEBUG',
         'propagate': True,
      },
   },
}

When you see the issue, it’s easier to handle than by blind debugging.

My issue was:

Invalid HTTP_HOST header: ‘pt_web:8000’. The domain name provided is not valid according to RFC 1034/1035.

I resolved it by adding proxy_set_header Host $host; to the Nginx config file and enabling port forwarding with USE_X_FORWARDED_PORT = True in the settings.py (it’s because in my case I was listening to requests on Nginx port 8080 and passing to guni on port 8000).

Josh Voigts's user avatar

Josh Voigts

4,1141 gold badge18 silver badges43 bronze badges

answered Jul 22, 2018 at 6:51

Yuseferi's user avatar

3

For me, I got this error by not setting USE_X_FORWARDED_HOST to true. From the docs:

This should only be enabled if a proxy which sets this header is in use.

My hosting service wrote explicitly in their documentation that this setting must be used, and I get this 400 error if I forget it.

answered Jul 30, 2016 at 22:41

ki9's user avatar

ki9ki9

5,1345 gold badges36 silver badges48 bronze badges

2

in the settings.py of your project, check line 28, where is the Allows Host

settings.py


ALLOWED_HOSTS = ['IP', 'servidor', ]

you must put the IP and the server you use, level local or web
settings.py

ALLOWED_HOSTS = ['127.0.0.1', 'localhost', 'www.ejemplo.com']

or

ALLOWED_HOSTS = ['*']

answered Feb 3, 2021 at 16:21

Martín Contreras's user avatar

I had the same problem and I fixed it by setting ALLOWED_HOSTS = ['*'] and to solve the problem with the static images you have to change the virtual paths in the environment configuration like this:

Virtual Path
                Directory

/static/                          /opt/python/current/app/yourpj/static/
/media/                        /opt/python/current/app/Nuevo/media/

I hope it helps you.

PD: sorry for my bad english.

Salem's user avatar

Salem

13.2k4 gold badges50 silver badges70 bronze badges

answered Jul 25, 2015 at 20:37

Jorge's user avatar

JorgeJorge

311 bronze badge

With DEBUG = False in you settings file, you also need ALLOWED_HOST list set up.
Try including ALLOWED_HOST = ['127.0.0.1', 'localhost', 'www.yourdomain.com']

Otherwise you might receive a Bad Request(400) error from django.

answered Apr 19, 2017 at 8:50

Abhishek Lodha's user avatar

Abhishek LodhaAbhishek Lodha

7272 gold badges7 silver badges30 bronze badges

For me as I have already xampp on 127.0.0.1 and django on 127.0.1.1
and i kept trying adding hosts

ALLOWED_HOSTS = ['127.0.0.1', 'localhost', 'www.yourdomain.com', '*', '127.0.1.1']

and i got the same error or (400) bad request
enter image description here

so I change the url to 127.0.1.1:(the used port)/project
and voila !

you have to check what is your virtual network address, for me as i use bitnami django stack 2.2.3-1 on Linux i can check which port django is using.
if you have an error ( 400 bad request ) then i guess django on different virtual network ..
good luck
enter image description here

SIGSTACKFAULT's user avatar

answered Jul 4, 2019 at 15:11

Muhamed Noaman's user avatar

I had to stop the apache server first.

(f.e. sudo systemctl stop httpd.service / sudo systemctl disable httpd.service).

That solved my problem besides editing the ‘settings.py‘ file

to ALLOWED_HOSTS = ['se.rv.er.ip', 'www.example.com']

answered Jun 11, 2020 at 6:09

Ingo Mi's user avatar

Ingo MiIngo Mi

95911 silver badges26 bronze badges

try manage.py collectstatic.
I was missing a static file after an update, hence the bad request.

answered Aug 6, 2021 at 16:55

run_the_race's user avatar

run_the_racerun_the_race

2,0262 gold badges32 silver badges54 bronze badges

There are two other reasons I know (less frequent but still) that can cause 400 bad request:

  1. Nginx Doesn’t Pass $Host To Your Application
  2. Host Name Has Underscores

I outlined these examples here

answered Apr 3 at 9:20

Andrii Zarubin's user avatar

Andrii ZarubinAndrii Zarubin

2,1551 gold badge18 silver badges31 bronze badges

Try to run your server with the --insecure flag, just like this:

python manage.py runserver --insecure

Paolo's user avatar

Paolo

19.8k21 gold badges71 silver badges113 bronze badges

answered Sep 14, 2019 at 20:59

Alberto's user avatar

Receiving a 400 Bad Request while accessing your own Django application can be frustrating.

This error in Django indicates that the server was unable to process the request sent by the client due to invalid syntax.

Here at Bobcares, we often receive requests on fixing Django errors as a part of our Server Management Services.

Today, let’s see how our Support Engineers fix this error.

How we fix Django 400 bad request error?

Now, let’s see how our Support Engineers go fixing this error.

1. Correct the Invalid syntax

Recently, one of our customers approached telling us that the Django server was running fine with DEBUG = True. But, when the DEBUG is set to False in the settings file, then the server will stop and throw the below error in the command prompt:

CommandError: You must set settings.ALLOWED_HOSTS if DEBUG is False.

After changing ALLOWED_HOSTS to,["http://127.0.0.1:8000",] in the browser, the customer received the below error

Bad Request (400)

Our Support Engineers started troubleshooting the error by setting the ALLOWED_HOSTS list to fully qualified hostnames.

We didn’t set the URL in it. And, omitted the port and the protocol.

ALLOWED_HOSTS = ['127.0.0.1', 'localhost']

Since our customer was using 127.0.0.1, we added localhost to the list too.

We can also use * to match any host:

ALLOWED_HOSTS = ['*']

Finally, this fixed the error.

2. Remove the Corrupt extensions in Django

We received another error from one of our customers. The error was as below.

[09/Nov/2010 00:31:53] "GET /acq/ HTTP/1.1" 200 1393
[09/Nov/2010 00:31:53] code 400, message Bad request syntax ('x16x03x00...')
?????????...." 400 -???8 .....?????
[09/Nov/2010 00:33:39] "GET /acq/ HTTP/1.1" 200 1393

Our Support Engineers started troubleshooting this error by clearing the cache.

Then, we tried uninstalling all the extensions.

That did the trick. After the uninstallation, the error disappeared. We finally found that the trouble was with the Secure Sites extension which lets to access a secure website.

Finally, we fixed this error by adding localhost to “Assumed no secure version exists” within the extension’s options.

3. Clearing the cache to fix 400 bad request error

The cache can also create problems. So we simply suggest our customers clear the cache and try checking if the error disappears.

Also, we suggest logging out from the framework and ask to login again and see if it works because the cached cookies will have the old login session.

[Need further assistance with Django errors? – We’ll help you]

Conclusion

In short, the Django 400 bad request is caused due to many reasons which include incorrect syntax, corrupt extensions, incorrect URL, and so on. Today, we saw how our Support Engineers fix it.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = «owonCMyG5nEQ0aD71QM»;

Исключения¶

Исключения… позволяют чисто организовать обработку ошибок в центральном или высокоуровневом месте в структуре программы.

‒ Doug Hellmann, Python Exception Handling Techniques

Представления фреймворка REST обрабатывают различные исключения и возвращают соответствующие ответы на ошибки.

Обрабатываемыми исключениями являются:

  • Подклассы APIException, поднятые внутри фреймворка REST.

  • Исключение Django Http404.

  • Исключение Django PermissionDenied.

В каждом случае фреймворк REST возвращает ответ с соответствующим кодом состояния и типом содержимого. В теле ответа будут содержаться любые дополнительные сведения о характере ошибки.

Большинство ответов на ошибки будут содержать ключ detail в теле ответа.

Например, следующий запрос:

DELETE http://api.example.com/foo/bar HTTP/1.1
Accept: application/json

Может быть получен ответ об ошибке, указывающий на то, что метод DELETE не разрешен на данном ресурсе:

HTTP/1.1 405 Method Not Allowed
Content-Type: application/json
Content-Length: 42

{"detail": "Method 'DELETE' not allowed."}

Ошибки валидации обрабатываются несколько иначе, и в качестве ключей в ответе будут указаны имена полей. Если ошибка валидации не относится к конкретному полю, то будет использован ключ «non_field_errors» или любое строковое значение, установленное для параметра NON_FIELD_ERRORS_KEY.

Пример ошибки валидации может выглядеть следующим образом:

HTTP/1.1 400 Bad Request
Content-Type: application/json
Content-Length: 94

{"amount": ["A valid integer is required."], "description": ["This field may not be blank."]}

Пользовательская обработка исключений¶

Вы можете реализовать пользовательскую обработку исключений, создав функцию-обработчик, которая преобразует исключения, возникающие в представлениях вашего API, в объекты ответа. Это позволит вам контролировать стиль ответов на ошибки, используемый вашим API.

Функция должна принимать пару аргументов, первый из которых — обрабатываемое исключение, а второй — словарь, содержащий любой дополнительный контекст, например, обрабатываемое в данный момент представление. Функция обработчика исключения должна либо вернуть объект Response, либо вернуть None, если исключение не может быть обработано. Если обработчик возвращает None, то исключение будет повторно поднято и Django вернет стандартный ответ HTTP 500 „server error“.

Например, вы можете захотеть убедиться, что все ответы на ошибки включают код состояния HTTP в теле ответа, например, так:

HTTP/1.1 405 Method Not Allowed
Content-Type: application/json
Content-Length: 62

{"status_code": 405, "detail": "Method 'DELETE' not allowed."}

Чтобы изменить стиль ответа, вы можете написать следующий пользовательский обработчик исключений:

from rest_framework.views import exception_handler

def custom_exception_handler(exc, context):
    # Call REST framework's default exception handler first,
    # to get the standard error response.
    response = exception_handler(exc, context)

    # Now add the HTTP status code to the response.
    if response is not None:
        response.data['status_code'] = response.status_code

    return response

Аргумент context не используется обработчиком по умолчанию, но может быть полезен, если обработчику исключения нужна дополнительная информация, например, обрабатываемое в данный момент представление, доступ к которому можно получить в виде context['view'].

Обработчик исключений также должен быть настроен в ваших настройках, используя клавишу настройки EXCEPTION_HANDLER. Например:

REST_FRAMEWORK = {
    'EXCEPTION_HANDLER': 'my_project.my_app.utils.custom_exception_handler'
}

Если параметр 'EXCEPTION_HANDLER' не указан, то по умолчанию используется стандартный обработчик исключений, предоставляемый фреймворком REST:

REST_FRAMEWORK = {
    'EXCEPTION_HANDLER': 'rest_framework.views.exception_handler'
}

Обратите внимание, что обработчик исключений будет вызываться только для ответов, сгенерированных поднятыми исключениями. Он не будет использоваться для любых ответов, возвращаемых непосредственно представлением, например, ответов HTTP_400_BAD_REQUEST, которые возвращаются общими представлениями при неудачной проверке сериализатора.


Справочник по API¶

APIException¶

Подпись: APIException()

базовый класс для всех исключений, возникающих внутри класса APIView или @api_view.

Чтобы обеспечить пользовательское исключение, подкласс APIException и установите атрибуты .status_code , .default_detail , и default_code на класс.

Например, если ваш API полагается на сторонний сервис, который иногда может быть недоступен, вы можете захотеть реализовать исключение для кода ответа HTTP «503 Service Unavailable». Это можно сделать следующим образом:

from rest_framework.exceptions import APIException

class ServiceUnavailable(APIException):
    status_code = 503
    default_detail = 'Service temporarily unavailable, try again later.'
    default_code = 'service_unavailable'

Проверка исключений API¶

Существует ряд различных свойств, доступных для проверки состояния исключения API. Вы можете использовать их для создания пользовательской обработки исключений в вашем проекте.

Доступными атрибутами и методами являются:

  • .detail — Возвращает текстовое описание ошибки.

  • .get_codes() — Возвращает идентификатор кода ошибки.

  • .get_full_details() — Возвращает как текстовое описание, так и идентификатор кода.

В большинстве случаев деталь ошибки будет простым элементом:

>>> print(exc.detail)
You do not have permission to perform this action.
>>> print(exc.get_codes())
permission_denied
>>> print(exc.get_full_details())
{'message':'You do not have permission to perform this action.','code':'permission_denied'}

В случае ошибок валидации деталь ошибки будет представлять собой список или словарь элементов:

>>> print(exc.detail)
{"name":"This field is required.","age":"A valid integer is required."}
>>> print(exc.get_codes())
{"name":"required","age":"invalid"}
>>> print(exc.get_full_details())
{"name":{"message":"This field is required.","code":"required"},"age":{"message":"A valid integer is required.","code":"invalid"}}

ParseError¶

Подпись: ParseError(detail=None, code=None)

Возникает, если запрос содержит неправильно сформированные данные при доступе к request.data.

По умолчанию это исключение приводит к ответу с кодом состояния HTTP «400 Bad Request».

AuthenticationFailed¶

Подпись: AuthenticationFailed(detail=None, code=None)

Возникает, когда входящий запрос содержит неправильную аутентификацию.

По умолчанию это исключение приводит к ответу с кодом состояния HTTP «401 Unauthenticated», но оно также может привести к ответу «403 Forbidden», в зависимости от используемой схемы аутентификации. Более подробную информацию см. в authentication documentation.

NotAuthenticated¶

Подпись: NotAuthenticated(detail=None, code=None)

Возникает, когда неаутентифицированный запрос не прошел проверку на разрешение.

По умолчанию это исключение приводит к ответу с кодом состояния HTTP «401 Unauthenticated», но оно также может привести к ответу «403 Forbidden», в зависимости от используемой схемы аутентификации. Более подробную информацию см. в authentication documentation.

PermissionDenied¶

Подпись: PermissionDenied(detail=None, code=None)

Возникает, когда аутентифицированный запрос не прошел проверку на разрешение.

По умолчанию это исключение приводит к ответу с кодом состояния HTTP «403 Forbidden».

NotFound¶

Подпись: NotFound(detail=None, code=None)

Возникает, когда ресурс не существует по заданному URL. Это исключение эквивалентно стандартному исключению Django Http404.

По умолчанию это исключение приводит к ответу с кодом состояния HTTP «404 Not Found».

MethodNotAllowed¶

Подпись: MethodNotAllowed(method, detail=None, code=None)

Возникает, когда происходит входящий запрос, который не сопоставлен с методом-обработчиком на представлении.

По умолчанию это исключение приводит к ответу с кодом состояния HTTP «405 Method Not Allowed».

Неприемлемо¶

Подпись: NotAcceptable(detail=None, code=None)

Возникает, когда поступает запрос с заголовком Accept, который не может быть удовлетворен ни одним из доступных рендереров.

По умолчанию это исключение приводит к ответу с кодом состояния HTTP «406 Not Acceptable».

Дросселированный¶

Подпись: Throttled(wait=None, detail=None, code=None)

Возникает, когда входящий запрос не проходит проверку на дросселирование.

По умолчанию это исключение приводит к ответу с кодом состояния HTTP «429 Too Many Requests».

ValidationError¶

Подпись: ValidationError(detail, code=None)

Исключение ValidationError несколько отличается от других классов APIException:

  • Аргумент detail является обязательным, а не опциональным.

  • Аргумент detail может представлять собой список или словарь сведений об ошибках, а также может быть вложенной структурой данных. Используя словарь, вы можете указать ошибки на уровне полей при выполнении проверки на уровне объектов в методе validate() сериализатора. Например. raise serializers.ValidationError({'name': 'Please enter a valid name.'})

  • По соглашению вы должны импортировать модуль serializers и использовать полностью квалифицированный стиль ValidationError, чтобы отличить его от встроенной ошибки валидации Django. Например. raise serializers.ValidationError('This field must be an integer value.')

Класс ValidationError следует использовать для сериализатора и валидации полей, а также классами валидаторов. Он также возникает при вызове serializer.is_valid с аргументом ключевого слова raise_exception:

serializer.is_valid(raise_exception=True)

Общие представления используют флаг raise_exception=True, что означает, что вы можете переопределить стиль ответов на ошибки валидации глобально в вашем API. Для этого используйте пользовательский обработчик исключений, как описано выше.

По умолчанию это исключение приводит к ответу с кодом состояния HTTP «400 Bad Request».


Общие представления ошибок¶

Django REST Framework предоставляет два представления ошибок, подходящих для предоставления общих JSON 500 Server Error и 400 Bad Request ответов. (Стандартные представления ошибок Django предоставляют ответы в формате HTML, что может не подойти для приложения, использующего только API).

Используйте их в соответствии с Django’s Customizing error views documentation.

rest_framework.exceptions.server_error

Возвращает ответ с кодом состояния 500 и типом содержимого application/json.

Установить как handler500 :

handler500 = 'rest_framework.exceptions.server_error'

rest_framework.exceptions.bad_request

Возвращает ответ с кодом состояния 400 и типом содержимого application/json.

Установить как handler400 :

handler400 = 'rest_framework.exceptions.bad_request'

Вернуться на верх

When you tries to do http post with a JSON data / payload to your Django REST Framework ( DRF ) based server, sometimes you may get the “Bad Request” error with http code 400 as below,

Bad Request: /YOUR_API/
[DD/Month/YEAR 19:31:51] "POST /users/ HTTP/1.1" 400 75

When we checked what is the meaning of this error from Django documentation, it is defined as,

“The 400 (bad request) – defaults.bad_request(request, exception, template_name=’400.html’)

When a SuspiciousOperation is raised in Django, it may be handled by a component of Django (for example resetting the session data). If not specifically handled, Django will consider the current request a ‘bad request’ instead of a server error.

django.views.defaults.bad_request, is otherwise very similar to the server_error view, but returns with the status code 400 indicating that the error condition was the result of a client operation. By default, nothing related to the exception that triggered the view is passed to the template context, as the exception message might contain sensitive information like filesystem paths.

bad_request views are also only used when DEBUG is False.”

When we checked, what this “SuspiciousOperation” is .. as per documentation it is defined as,

SuspiciousOperation – exception SuspiciousOperation[source][/source]
The SuspiciousOperation exception is raised when a user has performed an operation that should be considered suspicious from a security perspective, such as tampering with a session cookie.

Subclasses of SuspiciousOperation include: DisallowedHost , DisallowedModelAdminLookup, DisallowedModelAdminToField, DisallowedRedirect, InvalidSessionKey, RequestDataTooBig, SuspiciousFileOperation, SuspiciousMultipartForm, SuspiciousSession, TooManyFieldsSent

If a SuspiciousOperation exception reaches the WSGI handler level it is logged at the Error level and results in a HttpResponseBadRequest.

Now, coming back to our issues where we seen this “Bad Request” as mentioned earlier, lets try to do http POST to our server,

$ curl -v -k -H "Accept: application/json" -H Content-Type:application/json -X POST -d '{"username":lynxbee,"email":social(at)lynxbee.com,"age":"23"}' http://192.168.0.105:8000/users/
Note: Unnecessary use of -X or --request, POST is already inferred.
*   Trying 192.168.0.105...
* TCP_NODELAY set
* Connected to 192.168.0.105 (192.168.0.105) port 8000 (#0)
> POST /users/ HTTP/1.1
> Host: 192.168.0.105:8000
> User-Agent: curl/7.58.0
> Accept: application/json
> Content-Type:application/json
> Content-Length: 57
> 
* upload completely sent off: 57 out of 57 bytes
< HTTP/1.1 400 Bad Request
< Date: Sat, 15 Aug 2020 16:45:24 GMT
< Server: WSGIServer/0.2 CPython/3.6.9
< Content-Type: application/json
< Vary: Accept, Cookie
< Allow: GET, POST, HEAD, OPTIONS
< X-Frame-Options: DENY
< Content-Length: 75
< X-Content-Type-Options: nosniff
< Referrer-Policy: same-origin
< 
* Connection #0 to host 192.168.0.105 left intact

Notice above that when we sent http POST with the JSON , we got in reply “< HTTP/1.1 400 Bad Request” .. now if we closely look at the JSON and the error we received, we can see that we are missing double quote in JSON ‘{“username”:lynxbee,”email”:sociallynxbee.com,”age”:”23″}’ .. for username and email..

hence now lets correct the JSON by adding double quote for “lynxbee” and “social(at)lynxbee.com” ‘{“username”:”lynxbee”,”email”:”social(at)lynxbee.com”,”age”:”23″}’ and do http POST again,

$ curl -v -k -H "Accept: application/json" -H Content-Type:application/json -X POST -d '{"username":"lynxbee","email":"social(at)lynxbee.com","age":"23"}' http://192.168.0.105:8000/users/
Note: Unnecessary use of -X or --request, POST is already inferred.
*   Trying 192.168.0.105...
* TCP_NODELAY set
* Connected to 192.168.0.105 (192.168.0.105) port 8000 (#0)
> POST /users/ HTTP/1.1
> Host: 192.168.0.105:8000
> User-Agent: curl/7.58.0
> Accept: application/json
> Content-Type:application/json
> Content-Length: 61
> 
* upload completely sent off: 61 out of 61 bytes
< HTTP/1.1 201 Created
< Date: Sat, 15 Aug 2020 16:45:48 GMT
< Server: WSGIServer/0.2 CPython/3.6.9
< Content-Type: application/json
< Vary: Accept, Cookie
< Allow: GET, POST, HEAD, OPTIONS
< X-Frame-Options: DENY
< Content-Length: 61
< X-Content-Type-Options: nosniff
< Referrer-Policy: same-origin
< 
* Connection #0 to host 192.168.0.105 left intact
{"username":"lynxbee","email":"social(at)lynxbee.com","age":"23"}

And we can see the error has gone..

So final solution for this error

Most probably “Bad Request” error is sent when something is wrong from the data received from client to Django Server, hence observe clearly for any mistakes in data sent or http requests sent to server and correct. For example, as we were trying to send invalid json to server hence we got “Bad Request”

Понравилась статья? Поделить с друзьями:
  • Dismhost exe ошибка приложения
  • Django обработка ошибок формы
  • Dism ошибка 87 неизвестный параметр cleanup image
  • Django логирование ошибок
  • Dism ошибка 87 неизвестный параметр capture image