Search code examples
pythondjangodjango-admindjango-middlewaredjango-upgrade

Can't view django 2.0 admin page after upgrading


I've just been porting to Python3 from 2 and upgrading Django from 1.7 to 2.0 (massive changes I know). I'm using Heroku to host the app.

When I run heroku local or just run the app locally with manage.py runserver the app loads but navigating to the /admin page comes up with the error:

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:5000/admin
Using the URLconf defined in loowatt.urls, Django tried these URL patterns, in this order:

write/
admin/
^$ [name='index']
The current path, admin, didn't match any of these.

My new app.urls.py looks like this:

  1 from django.contrib import admin
  2 from django.urls import include, path
  3
  4 from . import views
  5
  6 urlpatterns = [
  7     path('write/', views.write),
  8     path('admin/', admin.site.urls),
  9     path('', include('units.urls')),
 10 ]

And my settings have all the correct middleware and context processors for the admin settings:

 21 INSTALLED_APPS = [
 22     'django.contrib.admin',
 23     'django.contrib.auth',
 24     'django.contrib.contenttypes',
 25     'django.contrib.sessions',
 26     'django.contrib.messages',
 27     'units.apps.UnitsConfig',
 28     # Disable Django's own staticfiles handling in favour of WhiteNoise, for
 29     # greater consistency between gunicorn and `./manage.py runserver`. See:
 30     # http://whitenoise.evans.io/en/stable/django.html#using-whitenoise-in-development
 31 #    'whitenoise.runserver_nostatic',
 32     'django.contrib.staticfiles',
 33     'import_export',
 34     'rangefilter',
 35 ]

 37 MIDDLEWARE_CLASSES = [
 38     'django.middleware.security.SecurityMiddleware',
 39     'whitenoise.middleware.WhiteNoiseMiddleware',
 40     'django.contrib.sessions.middleware.SessionMiddleware',
 41     'django.middleware.common.CommonMiddleware',
 42     'django.middleware.csrf.CsrfViewMiddleware',
 43     'django.contrib.auth.middleware.AuthenticationMiddleware',
 44     'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 45     'django.contrib.messages.middleware.MessageMiddleware',
 46     'django.middleware.clickjacking.XFrameOptionsMiddleware',
 47 ]

 53 TEMPLATES = [
 54     {
 55         'BACKEND': 'django.template.backends.django.DjangoTemplates',
 56         'DIRS': [],
 57         'APP_DIRS': True,
 58         'OPTIONS': {
 59             'context_processors': [
 60                 'django.template.context_processors.debug',
 61                 'django.template.context_processors.request',
 62                 'django.contrib.auth.context_processors.auth',
 63                 'django.contrib.messages.context_processors.messages',
 64             ],
 65             'debug': DEBUG,
 66         },
 67     },
 68 ]

Anyone have any ideas of what they'd try next to get it working?


Solution

  • You haven't switched from the old-style middleware MIDDLEWARE_CLASSES to the new-style middleware MIDDLEWARE. You get the 404 because your project is defaulting to MIDDLEWARE = [], so the redirect to append the slash (e.g /admin -> `/admin/) isn't happening).

    Note that Django 1.7 to 2.0 is a big jump. You may find it easier to go via 1.8 and 1.11 (which is LTS and still supported) first. In this case, Django 1.11 supports MIDDLEWARE_CLASSES and MIDDLEWARE, so you can get your app working on Django 1.11 with MIDDLEWARE_CLASSES, switch to MIDDLEWARE to fix the deprecation warning, and then you're in a better position to upgrade to Django 2.0.

    Note that you can still use url() in Django 2.0, so you don't have to rewrite your URL patterns to use path() until you've got the rest of the project working.