简体   繁体   中英

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?

I think you have to register your models to the admin. I normally do it like this if it's a simple model.

    from django.contrib import admin
    from .models import *

    # Register your models here.
    @admin.register(Model)
    class ModelAdmin(admin.ModelAdmin): pass

I gave up trying to diagnose my problem, and nuked the project and started over. It works fine now. I'm honestly not sure what the problem was. If anyone is curious, here are my new files:

from settings.py:

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

from urls.py:

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

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

from admin.py:

from django.contrib import admin

# Register your models here.
from .models import HomePageText

admin.site.register(HomePageText)

I honestly don't understand why it wasn't working before. If anyone with more insight can spot some difference that I can't please let me know.

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)

Stupid and obvious suggestion, but Django admin changes don't always appear to be picked up immediately. If everything appears to be configured correctly (models and admins registered, apps installed etc), try a server restart - after all the suggestions above, ultimately, this did it for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM