Aidan Ewen
Aidan Ewen

Reputation: 13308

Why is admin.py never loaded?

I'm setting up the django admin on a project.

I've created admin.py files in each of my apps (just as I have on previous projects). However the admin.py modules are not being loaded. My models aren't registered and I can't break into the code with pydevd.settrace().

If I move my admin code to the end of models.py everything works as expected, the models are registered with admin, and the code runs (I can step through it with pydevd.settrace()).

So this fails -

my_project_app
    __init__.py
    cart
        __init__.py
        models.py
        admin.py

But when I add my code to the end of the models.py file everything runs fine -

from django.contrib import admin

class CartAdmin(admin.ModelAdmin):
    pass

admin.site.register(Cart, CartAdmin)

Obviously I'm going to be configuring the admin so I don't want everything in one module. How do I get my admin.py file working? And why have they stopped working (this is the first time I've used django 1.5 - not sure if that's relevant)

Upvotes: 4

Views: 490

Answers (2)

vibhor
vibhor

Reputation: 818

In your admin.py file you have to import your models

from cart.models import Cart

If still it didnot work then Check if you have included

'django.contrib.admin',

in your installed_apps list of settings file then in urls file include

admin.autodiscover()

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

run manange.py syncdb

run manage.py runserver

check 127.0.0.1:8000/admin

It should work

Upvotes: 3

Pavel Anossov
Pavel Anossov

Reputation: 62908

Usually you call admin.autodiscover() in your urls.py and it loads everything:

# urls.py
from django.conf.urls import patterns, include
from django.contrib import admin

admin.autodiscover()

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

Since you have to do it before trying to use admin.site.urls, that's the best place to call it.

Upvotes: 6

Related Questions