Python Django在调试为false时为未知URL提供500而不是404

Python Django在调试为false时为未知URL提供500而不是404,python,django,django-templates,http-status-code-404,Python,Django,Django Templates,Http Status Code 404,Django=2.1.x Python=3.7.x 如果Debug为True,则返回404 如果Debug为False,则给出500错误 我的project.url文件如下所示: urlpatterns = [ path("admin/", admin.site.urls), path("", app1.views.log_in, name="log_in"), path("log_in/", app1.views.log_in, name="log_in"),

Django=2.1.x

Python=3.7.x

如果Debug为True,则返回404

如果Debug为False,则给出500错误

我的
project.url
文件如下所示:

urlpatterns = [
    path("admin/", admin.site.urls),
    path("", app1.views.log_in, name="log_in"),
    path("log_in/", app1.views.log_in, name="log_in"),
    path("logout/", app1.views.log_out, name="logout"),
    path("launcher/", app1.views.launcher, name="launcher"),
    path("app2/", include("app2.urls")),
    path("app3/", include("app3.urls")),
]
Project_directory
    static_directory
        ...js files and css files and such...
    templates_directory
        400.html
        403.html
        404.html
        500.html
        base.html (all apps extend this page, which works great)
    project_directory
        urls.py
        settings.py
        ...other files...
    app1_directory
        views.py
        models.py
        templates_directory
            app1
                ...template files...
        ...other app1 files/directories...
    app2_directory
        ...app2 directories and files...
    app3_directory
        ...app3 directories and files...
# These two settings are only for testing purposes and are different
# In production
DEBUG = False
ALLOWED_HOSTS = ["*"]

ROOT_URLCONF = "project.urls"

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        # DIRS lets the apps extend base.html
        "DIRS": [os.path.join(BASE_DIR, "templates")],
        "APP_DIRS": True,
        "OPTIONS": {
            "context_processors": [
                "django.template.context_processors.debug",
                "django.template.context_processors.request",
                "django.contrib.auth.context_processors.auth",
                "django.contrib.messages.context_processors.messages",
                "project.context_processors.app_context",
                "project.context_processors.registrations",
            ]
        },
    }
]

STATIC_URL = "/static/"

STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static")

SESSION_EXPIRE_AT_BROWSER_CLOSE = True

if DEBUG:
    STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")]
我的目录结构如下所示:

urlpatterns = [
    path("admin/", admin.site.urls),
    path("", app1.views.log_in, name="log_in"),
    path("log_in/", app1.views.log_in, name="log_in"),
    path("logout/", app1.views.log_out, name="logout"),
    path("launcher/", app1.views.launcher, name="launcher"),
    path("app2/", include("app2.urls")),
    path("app3/", include("app3.urls")),
]
Project_directory
    static_directory
        ...js files and css files and such...
    templates_directory
        400.html
        403.html
        404.html
        500.html
        base.html (all apps extend this page, which works great)
    project_directory
        urls.py
        settings.py
        ...other files...
    app1_directory
        views.py
        models.py
        templates_directory
            app1
                ...template files...
        ...other app1 files/directories...
    app2_directory
        ...app2 directories and files...
    app3_directory
        ...app3 directories and files...
# These two settings are only for testing purposes and are different
# In production
DEBUG = False
ALLOWED_HOSTS = ["*"]

ROOT_URLCONF = "project.urls"

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        # DIRS lets the apps extend base.html
        "DIRS": [os.path.join(BASE_DIR, "templates")],
        "APP_DIRS": True,
        "OPTIONS": {
            "context_processors": [
                "django.template.context_processors.debug",
                "django.template.context_processors.request",
                "django.contrib.auth.context_processors.auth",
                "django.contrib.messages.context_processors.messages",
                "project.context_processors.app_context",
                "project.context_processors.registrations",
            ]
        },
    }
]

STATIC_URL = "/static/"

STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static")

SESSION_EXPIRE_AT_BROWSER_CLOSE = True

if DEBUG:
    STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")]
当我
python manage.py运行服务器时,我点击了一个我知道不存在的URL(比如
http://project/randomtrash.php
)如果
DEBUG=True

如果
DEBUG=False
,则点击相同的URL将得到
500
,并显示
500.html

my
settings.py的重要部分如下所示:

urlpatterns = [
    path("admin/", admin.site.urls),
    path("", app1.views.log_in, name="log_in"),
    path("log_in/", app1.views.log_in, name="log_in"),
    path("logout/", app1.views.log_out, name="logout"),
    path("launcher/", app1.views.launcher, name="launcher"),
    path("app2/", include("app2.urls")),
    path("app3/", include("app3.urls")),
]
Project_directory
    static_directory
        ...js files and css files and such...
    templates_directory
        400.html
        403.html
        404.html
        500.html
        base.html (all apps extend this page, which works great)
    project_directory
        urls.py
        settings.py
        ...other files...
    app1_directory
        views.py
        models.py
        templates_directory
            app1
                ...template files...
        ...other app1 files/directories...
    app2_directory
        ...app2 directories and files...
    app3_directory
        ...app3 directories and files...
# These two settings are only for testing purposes and are different
# In production
DEBUG = False
ALLOWED_HOSTS = ["*"]

ROOT_URLCONF = "project.urls"

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        # DIRS lets the apps extend base.html
        "DIRS": [os.path.join(BASE_DIR, "templates")],
        "APP_DIRS": True,
        "OPTIONS": {
            "context_processors": [
                "django.template.context_processors.debug",
                "django.template.context_processors.request",
                "django.contrib.auth.context_processors.auth",
                "django.contrib.messages.context_processors.messages",
                "project.context_processors.app_context",
                "project.context_processors.registrations",
            ]
        },
    }
]

STATIC_URL = "/static/"

STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static")

SESSION_EXPIRE_AT_BROWSER_CLOSE = True

if DEBUG:
    STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")]

Django在debug=True时呈现调试页面,忽略404页面


请参见

这与
应用程序上下文
注册
上下文处理器有关

在这些测试中,他们使用了
请求
并解决了与之相反的问题(即
解析(request.path).app_name
),而这些问题在
404
或其他错误上永远不会匹配,从而导致响应中出现
500
错误

目前,我已将这两个函数中的每一个都封装在它自己的简单if语句中:

if request.resolver_match:
    ...do stuff...

现在,所有错误都按预期正确呈现。

是的,这就是我知道它注册了404的原因。当我将其设置为False时,它给出了一个
500
错误,不再注册为404