Django 1.3.7 TemplateDoesNotExist error admin/index.html

时间:2016-10-20 19:18:13

标签: python django ubuntu

There are numerous questions asked on this site similar to this one, but I couldn't find one that could explain this particular behavior.

I am relatively new to django and ubuntu, so maybe the explanation is simple. This is an old (v1.3.7) django project I'm attempting to migrate to a new server.

Here is the full error I am getting in my apache log:

TemplateDoesNotExist at /admin/
admin/login.html
Request Method: GET
Request URL:    http://131.212.123.7:26080/glrimon/admin/
Django Version: 1.3.7
Exception Type: TemplateDoesNotExist
Exception Value:    
admin/login.html
Exception Location: /.../leave_beave/lib/python2.7/site-packages/django/template/loader.py in find_template, line 138
Python Executable:  /.../leave_beave/bin/python

leave_beave is the name of my virtualenv.

Here is the info passed along by the traceback:

Template-loader postmortem

Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
/glri_mon/siteweb/templates/admin/login.html (File does not exist)
Using loader django.template.loaders.app_directories.Loader:
/glri_mon/siteweb/gps_upload/templates/admin/login.html (File does not exist)
/glri_mon/siteweb/siteapp/templates/admin/login.html (File does not exist)
/glri_mon/siteweb/glrimon_models/templates/admin/login.html (File does not exist)
/glri_mon/siteweb/sdde/templates/admin/login.html (File does not exist)
/glri_mon/siteweb/dv/templates/admin/login.html (File does not exist)
/glri_mon/siteweb/contains/templates/admin/login.html (File does not exist)
/glri_mon/siteweb/sqlwrite/templates/admin/login.html (File does not exist)
/.../leave_beave/lib/python2.7/site-packages/cms/templates/admin/login.html (File does not exist)
/.../leave_beave/lib/python2.7/site-packages/tinymce/templates/admin/login.html (File does not exist)
/.../leave_beave/lib/python2.7/site-packages/mptt/templates/admin/login.html (File does not exist)
/.../leave_beave/lib/python2.7/site-packages/menus/templates/admin/login.html (File does not exist)
/.../leave_beave/lib/python2.7/site-packages/cms/plugins/text/templates/admin/login.html (File does not exist)
/.../leave_beave/lib/python2.7/site-packages/cms/plugins/picture/templates/admin/login.html (File does not exist)
/.../leave_beave/lib/python2.7/site-packages/cms/plugins/link/templates/admin/login.html (File does not exist)
/.../leave_beave/lib/python2.7/site-packages/cms/plugins/file/templates/admin/login.html (File does not exist)
/.../leave_beave/lib/python2.7/site-packages/cms/plugins/snippet/templates/admin/login.html (File does not exist)
/glri_mon/siteweb/cmsplugin_rst/templates/admin/login.html (File does not exist)

glri_mon is the django project directory. Here's the path to the template in question: /.../leave_beave/django/contrib/admin/templates/admin/index.html. Any thoughts on why django didn't include that path in it's search?

Here are my template loaders from settings.py:

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
#     'django.template.loaders.eggs.Loader',
)

I received this same error for an earlier problem, and I solved it by creating a symlink from the templates directory in question (.../python2.7/site-packages/lib.../package/templates) to my /project_dir/templates directory, which is the one configured as my template directory in settings.py:

TEMPLATE_DIRS = (
    os.path.join(PATH, 'templates'),
) 

I did this at the time, since I thought it was a one-off hack solution sort of thing, but now I'm getting the same error for attempting to access the admin page of my site (http://.../admin/). This tells me I'm going about this wrong.

I suppose I could add a symlink to every template directory that generates this error, but that doesn't seem like a good idea for a production site.

What am I missing here?

3 个答案:

答案 0 :(得分:4)

较新版本的pip将为包装创建一个轮子,以加快下次使用时的安装速度。但是正因为如此,模板才被安装到虚拟环境的根目录中。

1.4.22-https://github.com/django/django/commit/3b324970e390a6dc4c373db036d6f27300d7fded中添加了一种禁用车轮创建的解决方法,但是在此之前的任何操作都会创建车轮文件,从而导致诸如模板丢失等问题。

对wheel文件的官方支持已添加到1.5.1-https://github.com/django/django/commit/4391718f26466f82f943e37368181d5c0520fa35

对于pip版本19,您可以使用pip install --no-binary Django Django==1.3.7跳过轮子的安装/创建。较早版本的pip的命令行参数可能略有不同。

答案 1 :(得分:2)

为了完整起见,我通过在settings.py中明确地将/admin模板的路径添加到TEMPLATE_DIRS来解决了这个问题:

TEMPLATE_DIRS = (
    os.path.join(PATH, 'templates'),
    "/.../leave_beave/django/contrib/admin/templates",
)

我怀疑这是django 1.3的目标,但它解决了我的问题。

答案 2 :(得分:0)

很明显,Django无法搜索管理模板! 可能是/.../leave_beave/django/contrib/admin/templates目录丢失的情况。但是这种可能性很小。

如果不是这种情况,并且您正在使用 Django 1.8 + ,则需要在项目设置中重新访问“模板”的设置。确保正确定义了TEMPLATE_LOADERSTEMPLATES

TEMPLATE_LOADERS = (
    'django.template.loaders.app_directories.Loader',
    'django.template.loaders.filesystem.Loader',
)

这些是Django的默认加载程序,无需明确定义。

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.static',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
] 

TEMPLATES设置中,需要检查APP_DIRS的值是否设置为True。如果将此值设置为False,则Django将无法查找应用程序级模板。因此,将无法发现管理模板。