Jarrod
Jarrod

Reputation: 1705

Admin Site: TemplateDoesNotExist at /admin/

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

Upvotes: 27

Views: 36514

Answers (16)

Moses Noel
Moses Noel

Reputation: 25

I recommend you include 'import_export' in your 'INSTALLED_APPS' in settings.py. As explained from documentation, django-import-export is a Django application and library for importing and exporting data with included admin integration. Therefore for template of application to be render on the browser, the name of application must be included in settings.py. Unless other, you will get an error 'Template does not exist'. Common sense, it is not possible to render the template of application, while the name of application is not not included in settings.py. So import_export is an application built in by Django, include it in settings.py.

Upvotes: -2

mmrs151
mmrs151

Reputation: 4072

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

Upvotes: 2

Mohammed Samir
Mohammed Samir

Reputation: 416

This issue may happen if you changed something in TEMPLATES in settings.py:

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',
        ],
    },
},]

I have stumbled with this issue, alhamdulillah, my mistake was solved when I changed the 'APP_DIRS': False, to 'APP_DIRS': True,

Upvotes: 2

4givN
4givN

Reputation: 3234

For my case, I have INSTALLED_APPS good, but just need to point inside TEMPLATE_DIRS the django admin template:

import os #Put at the starting line

PROJECT_DIR = os.path.dirname(__file__)

TEMPLATE_DIRS = (
    ... #Another folder,
    os.path.join(PROJECT_DIR, 'relative/path/to/virtualenv/my_env/django/contrib/admin/templates'),
)

And it works

Upvotes: 0

mseromenho
mseromenho

Reputation: 173

I'm adding an "answer" as I cannot with my current "reputation" add a comment.

Following Aaron solution:

In a project with Django 1.4 with docker with the same issue (accessing yourproject/admin/login.html or yourproject/admin/index.html). I used in python.dockerfile this, and solved:

"RUN pip install -r requirements.txt --no-cache-dir"

Before I did this, I verified entering inside the docker, that inside the folder usr/local/lib/python2.7/site-packages/django/contrib/admin/ didnt exist the folder /templates/.

I was reading this before coming to this post: https://www.mail-archive.com/[email protected]&q=subject:%22TemplateDoesNotExist+at+%5C%2Fadmin%5C%2F%22&o=newest&f=1

Upvotes: 0

Danil
Danil

Reputation: 5171

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',
            ],
        },
    },
]

Upvotes: 11

issam
issam

Reputation: 93

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',

Upvotes: 1

Chris
Chris

Reputation: 6184

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.

Upvotes: 2

chidimo
chidimo

Reputation: 2948

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.

Upvotes: 1

Konstantin
Konstantin

Reputation: 119

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.

Upvotes: 11

Abrie Nel
Abrie Nel

Reputation: 134

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'),
    ]
    

Upvotes: 11

Newton
Newton

Reputation: 71

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

Upvotes: -1

Syed Affan Aslam
Syed Affan Aslam

Reputation: 19

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!

Upvotes: 0

Armstrongya
Armstrongya

Reputation: 825

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.

Upvotes: 1

Aaron
Aaron

Reputation: 2679

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.

Upvotes: 32

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.

Upvotes: 6

Related Questions