【问题标题】:Django does not override template?Django不覆盖模板?
【发布时间】:2015-04-15 20:36:03
【问题描述】:

请帮忙。这段代码有什么问题?它不会覆盖 Django 1.7.3 中的管理模板。

settings.py

TEMPLATE_DIRS = (
                 os.path.join(BASE_DIR, 'templates').replace('\\','/'),
    )

TEMPLATE_LOADERS = (

    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',

)

templates/admin/base_site.html

{% extends "admin/base.html" %}

{% block title %}{{ title }} | {{ site_title|default:_('BOOKREADER site admin') }}{% endblock %}

{% block branding %}
<h1 id="site-name"><a href="{% url 'admin:index' %}">BOOKREADER administration</a></h1>
{% endblock %}

{% block nav-global %}{% endblock %}

【问题讨论】:

    标签: django


    【解决方案1】:

    确保您已设置文件系统模板加载器:

    # List of callables that know how to import templates from various sources.
    TEMPLATE_LOADERS = (
        'django.template.loaders.filesystem.Loader',
        'django.template.loaders.app_directories.Loader',
    )
    

    您也可以在settings.py 中检查该文件夹是否存在

     import os
     assert os.path.exists(TEMPLATE_DIRS[0])  # Will fail on startup if your template dir is not properly set up
    

    【讨论】:

    • 感谢您的回复。我的代码中有模板加载器。当我在 settings.py 的末尾添加断言时,它给出了 AssertionError。我真的卡住了
    • 在断言之前添加一个 Python 断点,检查 TEMPLATE_DIRS 的内容并修复它以匹配您的设置。
    • 如果你不熟悉如何调试 Python 程序,这里有一个 Python 命令行调试器入门的随机教程:gregaker.net/2012/apr/05/debugging-python-with-pdb-or-ipdb
    • 我使用 print 来检查我的路径。 1) print ("base dir path", BASE_DIR) -> C:\Users\Tulpor\workspace\tulpor 2) print ("new dir path", os.path.join(BASE_DIR, 'templates').replace(' \\','/')) -> C:/Users/Tulpor/workspace/tulpor/templates
    猜你喜欢
    • 2020-02-18
    • 2019-08-05
    • 2017-07-06
    • 2019-05-02
    • 2015-03-16
    • 2013-01-23
    • 2019-08-11
    • 1970-01-01
    • 2013-05-22
    相关资源
    最近更新 更多