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

那年仲夏 提交于 2019-11-28 21:00:50

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!