Skip to content
Advertisement

django 404 when loading admin but not local pages when deployed to heroku

I have just deployed my Django application to Heroku, All my locally saved apps load fine but not the django admin page

# mysite/urls.py
from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static
from django.conf import settings


urlpatterns = [
    path('', include('home.urls')),
    path('api/', include('api.urls')),
    path('admin/', admin.site.urls),
    # ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

When I go to the home page it loads and the same for all other URLs, except for the admin page at https://myapp.herokuapp.com/admin and you get the following error. Hosted on Heroku: enter image description here Hosted on local machine: enter image description here

In heroku logs there is the following message (when going to /admin/)

2021-07-20T07:11:19.735134+00:00 heroku[router]: at=info method=GET path="/admin/" host=codex-django.herokuapp.com request_id=95f13dbb-e715-4549-9546-2238777d1732 fwd="124.197.25.201" dyno=web.1 connect=1ms service=85ms status=302 bytes=405 protocol=https
2021-07-20T07:11:19.735051+00:00 app[web.1]: 10.31.110.160 - - [20/Jul/2021:19:11:19 +1200] "GET /admin/ HTTP/1.1" 302 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36"
2021-07-20T07:11:22.771314+00:00 heroku[router]: at=info method=GET path="/admin/login/?next=/admin/" host=codex-django.herokuapp.com request_id=2bc1abd1-d191-4363-be3d-788ab2f08645 fwd="124.197.25.201" dyno=web.1 connect=1ms service=2814ms status=500 bytes=151615 protocol=https
2021-07-20T07:11:22.762654+00:00 app[web.1]: Internal Server Error: /admin/login/
2021-07-20T07:11:22.762666+00:00 app[web.1]: Traceback (most recent call last):
2021-07-20T07:11:22.762667+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/sites/models.py", line 39, in _get_site_by_request
2021-07-20T07:11:22.762668+00:00 app[web.1]:     SITE_CACHE[host] = self.get(domain__iexact=host)
2021-07-20T07:11:22.762668+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/manager.py", line 85, in manager_method
2021-07-20T07:11:22.762669+00:00 app[web.1]:     return getattr(self.get_queryset(), name)(*args, **kwargs)
2021-07-20T07:11:22.762678+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/query.py", line 435, in get
2021-07-20T07:11:22.762679+00:00 app[web.1]:     raise self.model.DoesNotExist(
2021-07-20T07:11:22.762679+00:00 app[web.1]: django.contrib.sites.models.Site.DoesNotExist: Site matching query does not exist.
2021-07-20T07:11:22.762680+00:00 app[web.1]: 
2021-07-20T07:11:22.762680+00:00 app[web.1]: During handling of the above exception, another exception occurred:
2021-07-20T07:11:22.762681+00:00 app[web.1]: 
2021-07-20T07:11:22.762681+00:00 app[web.1]: Traceback (most recent call last):
2021-07-20T07:11:22.762681+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
2021-07-20T07:11:22.762682+00:00 app[web.1]:     response = get_response(request)
2021-07-20T07:11:22.762682+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/django/core/handlers/base.py", line 181, in _get_response
2021-07-20T07:11:22.762683+00:00 app[web.1]:     response = wrapped_callback(request, *callback_args, **callback_kwargs)
2021-07-20T07:11:22.762683+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
2021-07-20T07:11:22.762683+00:00 app[web.1]:     response = view_func(request, *args, **kwargs)
2021-07-20T07:11:22.762684+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/admin/sites.py", line 414, in login
2021-07-20T07:11:22.762684+00:00 app[web.1]:     return LoginView.as_view(**defaults)(request)
2021-07-20T07:11:22.762684+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/django/views/generic/base.py", line 70, in view
2021-07-20T07:11:22.762685+00:00 app[web.1]:     return self.dispatch(request, *args, **kwargs)
2021-07-20T07:11:22.762685+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/decorators.py", line 43, in _wrapper
2021-07-20T07:11:22.762685+00:00 app[web.1]:     return bound_method(*args, **kwargs)
2021-07-20T07:11:22.762687+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/django/views/decorators/debug.py", line 89, in sensitive_post_parameters_wrapper
2021-07-20T07:11:22.762687+00:00 app[web.1]:     return view(request, *args, **kwargs)
2021-07-20T07:11:22.762687+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/decorators.py", line 43, in _wrapper
2021-07-20T07:11:22.762688+00:00 app[web.1]:     return bound_method(*args, **kwargs)
2021-07-20T07:11:22.762688+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/decorators.py", line 130, in _wrapped_view
2021-07-20T07:11:22.762689+00:00 app[web.1]:     response = view_func(request, *args, **kwargs)
2021-07-20T07:11:22.762689+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/decorators.py", line 43, in _wrapper
2021-07-20T07:11:22.762690+00:00 app[web.1]:     return bound_method(*args, **kwargs)
2021-07-20T07:11:22.762690+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
2021-07-20T07:11:22.762691+00:00 app[web.1]:     response = view_func(request, *args, **kwargs)
2021-07-20T07:11:22.762691+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/auth/views.py", line 63, in dispatch
2021-07-20T07:11:22.762691+00:00 app[web.1]:     return super().dispatch(request, *args, **kwargs)
2021-07-20T07:11:22.762692+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/django/views/generic/base.py", line 98, in dispatch
2021-07-20T07:11:22.762692+00:00 app[web.1]:     return handler(request, *args, **kwargs)
2021-07-20T07:11:22.762692+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/django/views/generic/edit.py", line 133, in get
2021-07-20T07:11:22.762692+00:00 app[web.1]:     return self.render_to_response(self.get_context_data())
2021-07-20T07:11:22.762693+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/auth/views.py", line 97, in get_context_data
2021-07-20T07:11:22.762693+00:00 app[web.1]:     current_site = get_current_site(self.request)
2021-07-20T07:11:22.762694+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/sites/shortcuts.py", line 13, in get_current_site
2021-07-20T07:11:22.762694+00:00 app[web.1]:     return Site.objects.get_current(request)
2021-07-20T07:11:22.762695+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/sites/models.py", line 60, in get_current
2021-07-20T07:11:22.762695+00:00 app[web.1]:     return self._get_site_by_request(request)
2021-07-20T07:11:22.762696+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/sites/models.py", line 45, in _get_site_by_request
2021-07-20T07:11:22.762696+00:00 app[web.1]:     SITE_CACHE[domain] = self.get(domain__iexact=domain)
2021-07-20T07:11:22.762696+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/manager.py", line 85, in manager_method
2021-07-20T07:11:22.762697+00:00 app[web.1]:     return getattr(self.get_queryset(), name)(*args, **kwargs)
2021-07-20T07:11:22.762697+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/query.py", line 435, in get
2021-07-20T07:11:22.762697+00:00 app[web.1]:     raise self.model.DoesNotExist(
2021-07-20T07:11:22.762698+00:00 app[web.1]: django.contrib.sites.models.Site.DoesNotExist: Site matching query does not exist.
2021-07-20T07:11:22.763432+00:00 app[web.1]: 10.31.110.160 - - [20/Jul/2021:19:11:22 +1200] "GET /admin/login/?next=/admin/ HTTP/1.1" 500 151354 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36"

Advertisement

Answer

If you want /admin to point to the admin you should turn on the APPEND_SLASH setting, otherwise you need to navigate to /admin/ (note the trailing slash).

APPEND_SLASH

When set to True, if the request URL does not match any of the patterns in the URLconf and it doesn’t end in a slash, an HTTP redirect is issued to the same URL with a slash appended.

You can see in the debug output that there is a URL pattern for admin/, but the requested path is admin (without the trailing slash) so it won’t match.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement