为什么在更改 BASE_DIR 在 Django 中的位置后,我的页面的模板块没有加载?

Why doesn't the template block of my page load, after changing where BASE_DIR is located in Django?

我正在遵循 建议,它似乎破坏了我的模板加载。

我的目录声明 'settings.py':

BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates'),
                 os.path.join(BASE_DIR, 'gantt_charts\templates\gantt_charts')]
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'),
                    ]

Views.py 我的应用:

def project_list(request):
    projects = Project.objects.filter(tasks__isnull=False).order_by('updated_date').distinct()
    return render(request, 'project_list.html', {'projects':projects})

def project_detail(request,project_id):
    projects = get_object_or_404(Project,pk=project_id)
    return render(request,'project_detail.html',{'projects':projects})

我的应用程序urls.py

urlpatterns = patterns('',
    url(r'^(?:project)?/?$', views.project_list),
    url(r'^project/(?P<project_id>\d+)/\w{0,50}',views.project_detail),
)

我的project_list.html(住在gantt_charts\templates\gantt_charts里面):

{% extends 'base.html'%}
{% block content %}
{% load humanize %}
{% load staticfiles %}
    {% for project in projects|slice:":3"%}
        <div class="project">
            <h3>
                <a href="project/{{project.id}}/{{project.slug}}" title="Created {{project.created_date|naturaltime}}">{{project.title}}</a>
            </h3>
            <ul>
                {% for task in project.tasks.all|slice:":3" %}
                <li>{{task.title}}</li>
                {% empty %}
                <li>No tasks in this project</li>
                {% endfor %}
            </ul>
        </div>
    {% endfor %}
{% endblock content %}

还有我的 base.html 模板(位于 templates 中):

{% load humanize %}
{% load staticfiles %}
<html>
    <head>
    <!-- external includes -->
        <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
        <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
        <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>

    <!-- local includes -->
        <link rel="stylesheet" href="{% static 'css/site.css' %}">
        <title>gAnttlr - Gantt charts</title>
    </head>
    <body>
        <div class="page-header fixed-div">
            <h1>gAnttlr</h1> 
            <img src ="{%static 'svg/antler3.svg'%}" class="antler-icon fit-div" alt="Medium sized antler">
        </div>
        </div>
        <div class="content container">
            <div class="row">
                <div class="col-md-8">
                    {% block content %}
                    {% endblock %}
                </div>
            </div>
        </div>
        <div class="push"></div>
        <div id="footer" class="muted">
            <p>
                gAnttlr logo derived from 'Vectorised' by Yug (Nordisk familjebok (1911), vol.15, p.19 [1]) [Public domain], <a href="http://commons.wikimedia.org/wiki/File%3AKronhjortshornets_utveckling%2C_Nordisk_familjebok.svg">via Wikimedia Commons</a>
            </p>
        </div>
    </body>
</html>

这是我认为应该发生的事情:

  1. 当我转到指定为 r'^(?:project)?/?$ 的 url 时(localhost/localhost/project
    1. 找到它要查找views.project_list然后去那里,
  2. 看到它想要渲染 project_list.html,在 TEMPLATE_DIRS 上查找并找到它。
  3. project_list.html中看到它需要涉及base.html,在TEMPLATE_DIRS中查找并找到它。
  4. 将两个 html 文件拼接在一起,填写数据库查询的其他变量,然后加载。

我得到的只是 base.html 页面,除此之外别无其他。我不明白为什么会这样。我能想到的就是它试图在 'templates' 目录中找到 project_list.html,找不到它就放弃了。有趣的是,如果我将 base 的位置指定为 'base.html' 以外的任何位置(比如在目录前添加一个目录,那么它会抛出一个错误并且不会呈现任何内容。

为什么 django 不加载 project_list.html 来代替 {% block content %}{% endblock %} 元标签?

我遵循的建议是改变我的 BASE_DIR 指向的位置也有一个不幸的效果告诉 django 我的数据库也移动了,因为它是相对于 BASE_DIR.[=11 放置的=]

我已修复此问题并将其设为 'constant',与我的其他 *_DIR 变量位于同一位置,因此我可以跟踪它的变化。

我还在 for 循环中添加了一个 {% empty %} 块,以便将来提醒我。