Why does Django throw an exception whenever I enable admin.autodiscover()?
Here is my setup. I am using Django version 1.1.1 on Dreamhost, Python 2.4. The problem I am having is whenever I create a simple app and also have admin.autodiscover() enabled, Django will throw an exception. My setup:
from django.conf.urls.defaults import *
from testapp.views import HelloWorld
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^HelloWorld/$', HelloWorld),
# Uncomment the admin/doc line below and add 'django.contrib.admindocs'
# to INSTALLED_APPS to ena开发者_如何学Pythonble admin documentation:
(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
(r'^admin/', include(admin.site.urls)),
)
My settings.py looks like:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
'django.contrib.admindocs',
'testapp',
)
My testapp.views looks like:
from django.http import HttpResponse
def HelloWorld(request):
return HttpResponse("Hello world")
If I comment out admin.autodiscover() I can get to my view of HelloWorld. If I enable admin.autodiscover() Django throws an exception that I am not able to trap.
Does anyone know why this might be happening and what I can do to fix it?
I've had a similar issue when I've renamed an app. Basically, if you've launched the app and used the admin using admin.autodiscover()
in your urls.py
file, it will cause an admin.pyc
file to be created in your app folder. Delete this admin.pyc
file and run the server again...and voila!
I'm going to guess that testapp/admin.py does not import the models.Model class you are creating admin for. Try the following:
./manage.py shell # you may immediately get a stack trace
>> import testapp.admin # I'll bet it blows up.
Old question, but still relevant. I had a similar problem that stumped me today. The issue was that I had refactored a set of files in a directory within a Django app folder (lib/cache
) into a single file (lib/cache.py
). Because there was still an __init__.py
in the cache
directory, Python was seeing the empty directory as a module and preventing access to cache.py
.
Commenting out admin.autodiscover()
in my urls.py
made it a bit easier to track this down but it still took some guesswork.
精彩评论