Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 203
Aidan Ewen Avatar asked Oct 22 '25 05:10

Aidan Ewen


2 Answers

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.

like image 165
Pavel Anossov Avatar answered Oct 23 '25 18:10

Pavel Anossov


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

like image 30
vibhor Avatar answered Oct 23 '25 19:10

vibhor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!