0

我已经在我的系统中实现了 django-hosts 库。当我访问我域中的无效 URL 时,它将显示 404 页面并且一切正常。但是当我在我的子域中访问一个无效的 URL 时,它会引发 500 错误不去或 404。

基础.py

ROOT_HOSTCONF = 'core.hosts'
DEFAULT_HOST='default'

主机.py

from django_hosts import patterns, host
from django.conf import settings

host_patterns = patterns('',
    host(r'', settings.ROOT_URLCONF, name='default'),
    host(r'api', 'api.urls', name='api'),
)

api.urls.py

from django.urls import path
from django.conf import settings
from django.conf.urls import url
from django.urls import path, include
from django.conf.urls.static import static
from .views import *

app_name = 'api'
urlpatterns = [
    url(r'^$', home.as_view(), name="home_api"),
    path('<slug:uid>/<slug:conference_id>', conference.as_view(), name="api_conference"),
]

if settings.DEBUG:
    import debug_toolbar
    urlpatterns += [
        url(r'^__debug__/', include(debug_toolbar.urls)),
    ]
    
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

当我请求http://api.mydomain.com/favicon.ico时,它显示

Traceback (most recent call last):
  File "/home/eps64/.local/share/virtualenvs/my-project-aV6d6yYu/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/eps64/.local/share/virtualenvs/my-project-aV6d6yYu/lib/python3.6/site-packages/django/core/handlers/base.py", line 100, in _get_response
    resolver_match = resolver.resolve(request.path_info)
  File "/home/eps64/.local/share/virtualenvs/my-project-aV6d6yYu/lib/python3.6/site-packages/django/urls/resolvers.py", line 558, in resolve
    raise Resolver404({'tried': tried, 'path': new_path})
django.urls.exceptions.Resolver404: {'tried': [[<URLPattern '^$' [name='home_api']>], [<URLPattern '<slug:uid>/<slug:conference_id>' [name='api_conference']>]], 'path': 'favicon.ico'}

如何处理子域中的无效 URL 请求?

4

1 回答 1

0

关于如何设置自定义错误视图的官方文档: https ://docs.djangoproject.com/en/stable/topics/http/views/#customizing-error-views

请将处理程序放入您的 api.urls.py

于 2020-09-11T11:09:48.950 回答