Registered models do not show up in admin

2019-02-08 05:55发布

问题:

I added a model to admin via admin.site.register, and it does not show up in admin. Since admin is so "It just works", I have no idea of how to debug this. Pointers?

回答1:

After adding and registering your admin:

# app/admin.py
class YourModelAdmin(admin.ModelAdmin):
    pass

admin.site.register(YourModel, YourModelAdmin)

Make sure your app is in your project settings.py:

# settings.py
INSTALLED_APPS = (
    # other apps ...
    'app',
)

Sync your project for that model if you have not done so already:

python manage.py syncdb

Restart your server, CTRL-C:

python manage.py runserver


回答2:

In such a situation is also a good practice to check if the user logged in to the admin panel has rights to manage such a model. If they do then you could change your code to access the functions as root.



回答3:

When in doubt, shut down server, syncdb, start server.



回答4:

I have the experience, that sometimes after changing an admin.py the dev-sever won't be restarted. in that case touch settings.py helps.



回答5:

I think the checklist in Thierry's answer is almost definitive, but make sure that urls.py contains admin.autodiscover() to load INSTALLED_APPS admin.py modules.

# urls.py
from django.conf.urls.defaults import *
from django.contrib import admin

admin.autodiscover()

urlpatterns = patterns('',
    ('^admin/', include(admin.site.urls)),
)

More info in the django docs.



回答6:

Have you added the application to your installed apps? That has happened to me both one and two times. :) Otherwise it would be useful for us to see the code to help you.



回答7:

Also make sure there are no syntax errors in your admin.py or anything. That can cause an app to fail to be registered with the AdminSite.



回答8:

comment out the some lines in urls.py see docs for more details

admin.autodiscover()

urlpatterns = patterns('',
    ('^admin/', include(admin.site.urls)),
)