Search code examples
pythondjangodjango-modelsdjango-admin

models not showing on django admin


I'm just starting my first project using Django 1.11. I followed the same steps I've used on multiple Django 1.10 projects, but for some reason my models are not showing up on my localhost/admin site.

My INSTALLED_APPS from settings.py:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'home.apps.HomeConfig',
]

My admin.py:

from django.contrib import admin

from home.models import Home

# begin Admin Class Definitions
class HomeAdmin(models.ModelAdmin):

    fieldsets = [
        ('Title', {'fields': ['title']}),
        ('Publication Date', {'fields': ['pub_date']}),
        ('Home Page Text', {'fields': ['header', 'sub_header', 
         'link_text']}),
    ]
    list_display = ('title', 'pub_date')
    list_filter = ['pub_date']

admin.site.register(Home, HomeAdmin)

My [main_app]/urls.py:

from django.conf.urls import url, include
from django.contrib import admin

admin.autodiscover()

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', include('home.urls'))
]

But when I go to localhost:8000/admin the only things present are Groups and Users as if I hadn't registered any models at all.

I have run makemigrations and migrate, and I have tried putting admin.py within the app directory instead of the project directory. It's currently in the [project_name]/[project_name] directory (the one with settings.py, urls.py, and wsgi.py files).

Any suggestions?


Solution

  • When I use the Django admin always import in this form:

    from django.contrib import admin
    

    And inherint in the class:

    class HomeAdmin(admin.ModelAdmin):
    

    So your admin.py file would looke like this:

    from django.contrib import admin
    
    from home.models import Home
    
    # begin Admin Class Definitions
    class HomeAdmin(admin.ModelAdmin):
    
        fieldsets = [
            ('Title', {'fields': ['title']}),
            ('Publication Date', {'fields': ['pub_date']}),
            ('Home Page Text', {'fields': ['header', 'sub_header', 
             'link_text']}),
        ]
        list_display = ('title', 'pub_date')
        list_filter = ['pub_date']
    
    admin.site.register(Home, HomeAdmin)