Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: 400 bad request syntax - what does this message mean?

People also ask

What does it mean if it says bad request 400?

The HyperText Transfer Protocol (HTTP) 400 Bad Request response status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error (for example, malformed request syntax, invalid request message framing, or deceptive request routing).

What does it mean when it says bad request?

The 400 Bad Request error is an HTTP status code indicates that the request you sent to the webserver was malformed , in other words, the data stream sent by the client to the server didn't follow the rules. It means that the request itself has somehow incorrect or corrupted and the server couldn't understand it.


To address your actual question, this occurs if you're trying to access the django server over https. Switch back to http and that error will disappear.


I get this kind of error when I run:

manage.py runserver ...

instead of:

manage.py runfcgi ...

because I'm behind Nginx.

When you use runserver, it is listening for standard http web requests. When you use runfcgi, it is listening for a different type of request, using fastcgi protocol instead of plain http.


You could refactor this maintenance middleware to achieve the result, because it checks the user status BEFORE processing content requests, which seems more djangonostic..

import settings
from django.http import HttpResponseRedirect


class MaintenanceModeMiddleware(object):
    """
    Maintenance mode for django

    If an anonymous user requests a page, he/she is redirected to the
    maintenance page.
    """
    def process_request(self, request):

        is_login = request.path in (
            settings.LOGIN_REDIRECT_URL,
            settings.LOGIN_URL,
            settings.LOGOUT_URL,
            settings.MAINTENANCE_PATH,
        )
        if (not is_login) and settings.MAINTENANCE and (not request.user.is_authenticated()):
            return HttpResponseRedirect(settings.MAINTENANCE_PATH)
        return None