简体   繁体   English

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

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

I'm following this advice, and it seems to have broken my template loading. 我正在遵循建议,它似乎破坏了我的模板加载。

Directory declarations in my 'settings.py': 我的“ 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 for my app: 适用于我的应用的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})

My apps urls.py 我的应用urls.py

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

My project_list.html (which lives inside gantt_charts\\\\templates\\\\gantt_charts ): 我的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 %}

And my base.html template (which lives in templates ): 还有我的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>

Here's what I think should be happening: 这是我认为应该发生的事情:

  1. When I go to the url specified as r'^(?:project)?/?$ (either localhost/ or localhost/project ) 当我转到指定为r'^(?:project)?/?$的url时( localhost/localhost/project
    1. Find it wants to look up views.project_list and go there, 找到它想要查找views.project_list并去那里,
  2. See it wants to render project_list.html , look that up over the TEMPLATE_DIRS, and find it. 看到它想要渲染project_list.html ,在TEMPLATE_DIRS上查找并找到它。
  3. In project_list.html see it needs to involve base.html , look that up over the TEMPLATE_DIRS, and find it. project_list.html它需要包含base.html ,在TEMPLATE_DIRS上查找并找到它。
  4. Stitch the two html files together, fill in the other variables from database queries, and then load. 将两个html文件缝合在一起,填写数据库查询中的其他变量,然后加载。

What I get though is just the base.html page, and nothing else. 我得到的只是 base.html页面,而没有其他内容。 I can't see why that would be. 我不明白为什么会这样。 All I can think is that it's trying to find project_list.html in the 'templates' dir, not find it and just give up. 我能想到的是,它正在尝试在'templates'目录中找到project_list.html ,而不是找到它,而是放弃了。 Interestingly if I specify the location of base as anything but 'base.html' (like prepending a dir, then it throws an error up and doesn't render anything. 有趣的是,如果我将base的位置指定为'base.html'任何内容(例如在dir前面添加前缀),则会抛出错误并且不呈现任何内容。

Why isn't django loading project_list.html in place of the {% block content %}{% endblock %} meta-tags? django为什么不加载{% block content %}{% endblock %}元标记来代替project_list.html

The advice I followed was to change where my BASE_DIR pointed also had the unfortunate effect of telling django that my db had moved too, as that was placed relative to BASE_DIR. 我遵循的建议是更改BASE_DIR指向的位置,这还很不幸地告诉django我的数据库也已经移动了,因为它相对于BASE_DIR放置了。

I've fixed this and made it a 'constant' at the same place as my other *_DIR variables, so I can keep track of it changing. 我已经解决了这个问题,并将其与其他* _DIR变量放在同一位置,使它成为“常量”,因此我可以跟踪它的变化。

I've also added an {% empty %} block to the for loop, to alert me of this in the future. 我还在for循环中添加了{% empty %}块,以在将来提醒我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM