简体   繁体   English

Django教程:模板和视图

[英]Django Tutorial: Templates and Views

I'm in the process of completing the official Django tutorial and I'm stuck on part 3. Since templates are also used in the last part of part2, I will describe what I did: 我正在完成正式的Django教程,我被困在第3部分。由于模板也用在第2部分的最后部分,我将描述我做了什么:

Part 2 told me to "copy the template admin/base_site.html from within the default Django admin template directory in the source code of Django itself (django/contrib/admin/templates) into an admin subdirectory of whichever directory you're using in TEMPLATE_DIRS." 第2部分告诉我“将模板admin / base_site.html从Django本身的源代码(django / contrib / admin / templates)中的默认Django管理模板目录中复制到您正在使用的目录的admin子目录中TEMPLATE_DIRS“。

So I created a new directory "admin" that has the following relative path (note that where Django uses the directory name 'mysite', I use 'django_test' : /django_test/polls/templates/admin. I copied the base_site.html file into this directory. 所以我创建了一个新目录“admin”,它具有以下相对路径(注意Django使用目录名称'mysite',我使用'django_test':/ django_test / polls / templates / admin。我复制了base_site.html文件进入这个目录。

When I render the file in my local browser, it says: {% extends "admin/base.html" %} {% load i18n %} {% block title %}{{ title }} | {% trans 'Django site admin' %}{% endblock %} {% block branding %}{% trans 'Django administration' %}{% endblock %} {% block nav-global %}{% endblock %} 当我在本地浏览器中呈现文件时,它会显示: {% extends "admin/base.html" %} {% load i18n %} {% block title %}{{ title }} | {% trans 'Django site admin' %}{% endblock %} {% block branding %}{% trans 'Django administration' %}{% endblock %} {% block nav-global %}{% endblock %} {% extends "admin/base.html" %} {% load i18n %} {% block title %}{{ title }} | {% trans 'Django site admin' %}{% endblock %} {% block branding %}{% trans 'Django administration' %}{% endblock %} {% block nav-global %}{% endblock %}

Part 3 has me create an index.html file in a new subdirectory polls/index.html. 第3部分让我在新的子目录polls / index.html中创建一个index.html文件。 But when I load this file in my web browser (using localhost server), I simply see the html code instead of a bulleted list (see below). 但是当我在我的网络浏览器中加载这个文件(使用localhost服务器)时,我只看到html代码而不是项目符号列表(见下文)。

Note that I also edited TEMPLATE_DIRS in my settings.py file to tell Django that it can find index.html under /Users/myname/Sites/django_test/django_test/templates 请注意,我还在settings.py文件中编辑了TEMPLATE_DIRS,告诉Django它可以在/ Users / myname / Sites / django_test / django_test / templates下找到index.html

Below I will paste the code that my local server renders (instead of the bulleted list, which is what I want). 下面我将粘贴我的本地服务器呈现的代码(而不是项目符号列表,这是我想要的)。 Do you know why this code is being rendered, instead of the bulleted list? 你知道为什么这个代码被渲染,而不是项目符号列表?

<html>
<head><title>Test</title></head>

<body>
{% if latest_poll_list %}
    <ul>
    {% for poll in latest_poll_list %}
        <li><a href="/polls/{{ poll.id }}/">{{ poll.question }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No polls are available.</p>
{% endif %}

</body>
</html>

I don't know whether I'm making a mistake in how I'm organizing the files. 我不知道我是否在如何组织文件时犯了错误。 Might someone have an idea about what I'm doing wrong? 可能有人知道我做错了什么?

档

As you say in your comment, you're putting the file path into your browser. 正如您在评论中所说,您将文件路径放入浏览器中。 Naturally, then, you're going to see the text of the template, because you are bypassing Django completely and getting the browser to load the unrendered template from disk. 当然,你会看到模板的文本,因为你完全绕过Django并让浏览器从磁盘加载未渲染的模板。

As the tutorial describes, you need to ask Django to serve the template and render it, via its normal URL mechanism. 正如教程所描述的那样,您需要让Django通过其常规URL机制提供模板并进行渲染。 In the earlier part of that section, you went to localhost:8000/admin/ to see the admin site - this hasn't changed just because you've replaced a template. 在该部分的前面部分,您访问了localhost:8000/admin/以查看管理站点 - 由于您已经替换了模板,因此没有更改。 Go back to that address and you'll see your updated - and rendered - template. 回到那个地址,你会看到你的更新 - 和渲染 - 模板。

The django admin site is easy once you get the hang of it. 一旦掌握了它,django管理站点就很容易了。

The steps to take are: 要采取的步骤是:

-Uncomment the django admin site in your urls.py - 在urls.py中取消注释django管理站点

-Make the css available to the admin site by either copying the admin folder (inside django package) into the folder specified in STATIC_ROOT in your settings.py or making the diectory available on your PYTHONPATH - 通过将admin文件夹(在django包内)复制到settings.py中STATIC_ROOT中指定的文件夹或在PYTHONPATH中使用该文件,使管理站点可以使用css

In other words, you dont need to create a template for the admin site. 换句话说,您不需要为管理站点创建模板。 You will, however, need to create templates to access the views that you create in your project 但是,您需要创建模板以访问在项目中创建的视图

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

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