Admin Site: TemplateDoesNotExist at /admin/

2019-01-17 21:52发布

问题:

I'm following Django's official Tutorial 2 but for some reason cannot create an admin site despite following all the steps correctly to my understanding.

This is the error I get:

TemplateDoesNotExist at /admin/
admin/login.html
Request Method: GET
Request URL:    http://127.0.0.1:8000/admin/
Django Version: 1.3.1
Exception Type: TemplateDoesNotExist
Exception Value:    
admin/login.html
Exception Location: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/template/loader.py in find_template, line 138
Python Executable:  /Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
Python Version: 2.7.2
Python Path:    
['/Users/jcugley/Documents/Programming/Python/Django/mysite',
 '/Library/Python/2.7/site-packages/setuptools-0.6c11-py2.7.egg',
 '/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
 '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
 '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
 '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
 '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
 '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
 '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
 '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
 '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages',
 '/Library/Python/2.7/site-packages']
Server time:    Tue, 24 Jan 2012 18:40:03 -0600

The error occurs after I uncomment the following lines (commented):

### urls.py ###
from django.conf.urls.defaults import patterns, include, url

from django.contrib import admin # THIS LINE
admin.autodiscover() # THIS LINE

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)), # THIS LINE
)

If I comment them again it gets rid of the error.

I do have django.contrib.admin in my INSTALLED_APPS in settings.py

回答1:

Since it works on other people's machines, and since you have the app directories loader enabled, admin site enabled in INSTALLED_APPS, and that's all it should take for templates to be discovered (I mean, what more can one do?) - I can only assume something is wrong with your django installation.

This would be a good chance to start using virtualenvs and a fresh install of django to rule out your settings:

Fire up a terminal, navigate to your project directory (doesn't actually matter...)

pip install virtualenv # if you don't have it.

virtualenv --no-site-packages env 
# this creates a virtual python environment that doesn't include system packages

source env/bin/activate
# this forces your bash session to use that environment

pip install django
# installs a fresh copy of django to your new environment

cd path_to_my_django_project    
# now go to your site directory

python manager.py runserver
# visit admin site.


回答2:

I ran into the same problem, and I had to force pip to re-download django.

pip install -r requirements.txt --ignore-installed --force-reinstall --upgrade --no-cache-dir

Note: I know that the --no-cache-dir option is necessary, I'm not certain that the other options are all required.



回答3:

I am using Django Version 1.9.7 and when trying to add the admin_tools (menu and dashboard) to my application I had a similar issue. I found I had to do three things:

  1. Edit the INSTALLED_APPS option in settings.py as follows (note that the admin_tools come before django contrib, 'mines' is the name of my application):

    INSTALLED_APPS = [
        'admin_tools',
        'admin_tools.theming',
        'admin_tools.menu',
        'admin_tools.dashboard',
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'mines'
    ]
    
  2. Edit the TEMPLATE setting in the settings.py file as follows (note the 'loaders' option that got added, and that APP_DIRS are now set to false):

    TEMPLATES = [{
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': False,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
            'loaders': [
                'django.template.loaders.filesystem.Loader',
                'django.template.loaders.app_directories.Loader',
                'admin_tools.template_loaders.Loader',
            ],
        },
    }]
    
  3. And then finally I updated my urls.py file as follows (note the include for the admin_tools urls):

    from django.conf.urls import include,url
    from django.contrib import admin
    from mines.views import SummaryByMapIcon
    
    urlpatterns = [
        url(r'^admin_tools/', include('admin_tools.urls')),
        url(r'^admin/', admin.site.urls),
        url(r'^summarybymapicon$', SummaryByMapIcon, name='summarybymapicon'),
    ]
    


回答4:

I solved this same problem by reinstalling Django with the --no-cache-dir option:

pip uninstall django
pip install django --no-cache-dir

Solved thanks to the answer here.



回答5:

Had the same issue. Strangely I found that sometime the template and media is not copied from your django/contrib/admin. Therefore you need to copy them to your virtual env django directory.

i.e from your /venv/lib/python2.7/site-packages/django/contrib directory you need to

ln -s ~/Sites/your_dj_app/venv/django/contrib/admin/templates templates

and

ln -s ~/Sites/your_dj_app/venv/django/contrib/admin/media media

I am so glad that my problem is solved but so annoyed that I had to spend over an hour debugging it.

Hope you won't have to :)



回答6:

Inheriting a Django-application from an experienced magician, I wanted to understand the model by looking at it through Django-admin. As the app's model was not in Admin yet, I created an admin.py and ran into various problems. I tried All-Of-The-Above(TM) and found this:

  1. I got the paths confused. I put the admin.py file into the wrong directory. If you have something like .../app/app/app/app/app... make sure you know which app needs to hold the admin.py. I figured that out by putting crap into the file and notice, that it would not crash. Hence 'crap' was not executed and something was wrong with the file. Fixed.

  2. TemplateDoesNotExist on 'admin/index.html' The app's settings.py was configured to only run django.template.backends.jinja2.Jinja2 as TEMPLATES BACKEND. But Admin needs the standard django.template.backends.django.DjangoTemplates to make it work. I read, that it was possible to have two template-engines configured. I copied the standard-block from a different app. Fixed.

  3. TemplateDoesNotExist on 'admin/widgets/related_widget_wrapper.html' While Admin was now working, looking at individual objects was still broken. I searched for the missing template and found it well in place. Changing anything on my TEMPLATES setting would not improve anything. I manually inserted paths in 'DIRS': [], changed options, deleted the Jinja backend, nothing. It felt like was changing the wrong file again. Using the debugger, I found that even without configuring Jinja, it would still run Jinja. Finally, in settings.py, I spotted this:

    FORM_RENDERER = 'django.forms.renderers.Jinja2'
    

    and commenting it out got Admin going.

Not sure tho what it does with the rest of the application but for learning it's okay.



回答7:

I ran into similar problem trying to configure django-admin-tools for Django 2.0.2

Eventually I got it working. Here's my TEMPLATES settings.

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': False,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
            'loaders' : [
                'django.template.loaders.filesystem.Loader',
                'django.template.loaders.app_directories.Loader',
                'admin_tools.template_loaders.Loader',
                ]
        },
    },
]

This worked even as I overrode the default admin template.

Just be sure to take note of where to place the django-admin-tools apps. See @Abrie Nel's answer above.



回答8:

for windows user the admin_tools in the templates section should be place in the begining

   'loaders': [
            **'admin_tools.template_loaders.Loader',**    
            'django.template.loaders.filesystem.Loader',
            'django.template.loaders.app_directories.Loader',


回答9:

In my case, I had to change APP_DIRS from False to True

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]


回答10:

I meet the same error, after several times pip install and uninstall Django, it's still not work. Then I download Django tar.gz file from Django website, and install from this file , all is fine. Hopefully this can help somebody.



回答11:

Despite their usefulness, the aforementioned answers are partial. I opine that the developer should know what is going on at the back-end, viz., "how" is this exception being raised.

As one can see in the exception that the Exception is TemplateDoesNotExist and the value name is the path, and something going wrong with the "loader.py" file. Although I was using Django 1.1.4, I am pretty sure the exception is being raised because of the line (in loader.py) where os.path.isdir(template_dir) is being checked. It cannot find the template folder(directory) in the django folder, which is located in the site-packages folder in the python's Lib folder.

Intuitively one can say it is because of the inappropriate installation of django. It is, however, a good exercise to find out the cause of the exception by rummaging through the source. When this error occured in my project, I did not reinstall django, instead I copied the folders from root (C:/Python27/django/contrib) -- yes I am using Windows -- to its counterpart in site-packages and it worked!



回答12:

I think there are some packages that you didn't install in INSTALLED_APPS, in my case I installed 'import_export' since I used 'importexportmodels