[英]Django giving 500 instead of 404 for unknown URLs when debug is false
Django = 2.1.x Django = 2.1.x
Python = 3.7.x Python = 3.7.x
If Debug is True - it returns a 404.如果 Debug 为 True - 它返回 404。
If Debug is False - it gives a 500 error.如果 Debug 为 False - 它会给出 500 错误。
My project.urls
file looks like this:我的project.urls
文件如下所示:
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")),
]
My directory structure looks like this:我的目录结构如下所示:
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...
When I python manage.py runserver
and I hit a URL I know doesn't exist (like http://project/randomtrash.php
) it gives an appropriate 404 if DEBUG = True
When I python manage.py runserver
and I hit a URL I know doesn't exist (like http://project/randomtrash.php
) it gives an appropriate 404 if DEBUG = True
If DEBUG = False
then hitting that same URL will give a 500
and the 500.html
displays.如果DEBUG = False
则点击相同的 URL 将给出500
和500.html
显示。
Important parts of my settings.py
look like this:我的settings.py
的重要部分如下所示:
# 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 renders the debug page when DEBUG=True, ignoring the 404 page. Django 在 DEBUG=True 时渲染调试页面,忽略 404 页面。
See Show 404 page on django when DEBUG=True当 DEBUG=True 时,请参阅 django 上的显示 404 页面
This had to do with the app_context
and registrations
context_processors.这与app_context
和registrations
context_processors 有关。
In those they were using the request
and resolving things against it (ie resolve(request.path).app_name
) which would never match up on a 404
or other error - thus causing a 500
error in response.在那些他们使用request
并针对它解决问题(即resolve(request.path).app_name
)中,这将永远不会匹配404
或其他错误 - 从而导致500
错误响应。
I've wrapped each of those couple functions in it's own simple if statement for now:我现在已经将这两个函数中的每一个都包含在它自己的简单 if 语句中:
if request.resolver_match:
...do stuff...
Now all the errors render appropriately as expected.现在所有错误都按预期正确呈现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.