Raise Error 404 using django-hosts in Django

I have the following problem. I'm using django-hosts for subdomains like blog.example.com, es.example.com.

The problem is that there are urls where I manage id, such as: blog.example.com/url/id

And suppose the user doesn't touch anything, because everything is an OK 200 but if the user goes from clever and touches the id to one that doesn't exist, the site returns a 404.

But here comes the problem, when using django-hosts if I do a handler404 in the urls of the subdomain. It throws me an Error 500 and if I then do a handler500 in the urls of the subdomain, literally, it breaks the server, since then apache tells me that there is a problem in the server.

Here I leave my url.py of the subdomain.

from django.urls import path
from django.views.generic.base import TemplateView
from django.conf.urls import handler404, handler500
from django.conf import settings
from django.conf.urls.static import static
from django.contrib.sitemaps.views import sitemap

# MODULO PROPIO
from . import views
from .sitemaps import MapaDeSitio

sitemaps = {
    'blog': MapaDeSitio
}

urlpatterns = static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + [
    # SITIO
    path('', views.indexView, name='IndexView'),
    path('articulo/<str:url>/<int:id>', views.ArticuloView, name="ArticuloView"),
    

    # SEO
    path('robots.txt', views.RobotsView.as_view()),
    path('BingSiteAuth.xml', TemplateView.as_view(template_name="blog/BingSiteAuth.xml", content_type="text/xml")),
    path('sitemap.xml', sitemap, {'sitemaps': sitemaps}, name='django.contrib.sitemaps.views.sitemap'),
]


# MANEJO DE ERRORES HTTP
handler404 = views.Error404 # Error 404
handler500 = views.Error500 # Error 500

Here I leave the code of the hosts.py of the django-hosts:

from django.conf import settings

from django_hosts import patterns, host


host_patterns = patterns(
    '',
    host(r'www', settings.ROOT_URLCONF, name="www"),
    host(r'blog', 'blog.urls', name="blog"),

)

If anyone can help me, I will thank you.

I now tried to remove the codes to see if it fixed apache2 error 500: 'handler404 = views. Error404 # Error 404 handler500 = views. Error500#Error 500'

And if you solve them. Half time you place some handlerX throws the error. But always in the url file of the subdomain.

Back to Top