Html 如何将模板插入另一个模板?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10985950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 00:57:45  来源:igfitidea点击:

How do you insert a template into another template?

htmldjangodjango-templates

提问by WindowsMaker

I have a very basic template (basic_template.html), and want to fill in the with data formatted using another partial template. The basic_template.html might contain several things formatted using the partial template.

我有一个非常基本的模板 (basic_template.html),并且想要填写使用另一个部分模板格式化的数据。basic_template.html 可能包含一些使用部分模板格式化的内容。

How should I structure the code in views.py?

我应该如何构建views.py中的代码?

The reason I am doing this is that later on the will be filled using Ajax. Am I doing this right?

我这样做的原因是稍后将使用 Ajax 填充。我这样做对吗?

回答by Simeon Visser

You can do:

你可以做:

<div class="basic">
{% include "main/includes/subtemplate.html" %}    
</div>

where subtemplate.htmlis another Django template. In this subtemplate.htmlyou can put the HTML that would be obtained with Ajax.

subtemplate.html另一个 Django 模板在哪里。在此subtemplate.html你可以把会使用Ajax获得的HTML。

You can also include the template multiple times:

您还可以多次包含模板:

<div class="basic">
{% for item in items %}
    {% include "main/includes/subtemplate.html" %}    
{% endfor %}
</div>

回答by Wipqozn

You can do this using a block.Blocks are a Django Template tag which will override sections of a template you extend.I've included an example below.

您可以使用来执行此操作块是一个 Django 模板标签,它将覆盖您扩展的模板部分我在下面包含了一个例子。

basic_template.html

basic_template.html

<body>
{% block 'body' %}
{% endblock %}
</body>

template you want to include:(i.e. example.html)

您要包含的模板:(即example.html)

{% extends 'basic_template.html' %} 
{% block 'body' %}
/* HTML goes here */
{% endblock %}

views.py:

视图.py

return render_to_response(template='example.html', context, context_instance)

Doing this will load basic_template.html, but replace everything inside of {% block 'body' %} {% endblock %}in basic_template.html with whatever is contained within {% block 'body' %} {% endblock %}.

这样做将加载basic_template.html,但将basic_template.html 中的所有内容替换为{% block 'body' %} {% endblock %}中包含的任何内容{% block 'body' %} {% endblock %}

You can read more about blocks and template inheritance in the Django Docs

您可以在Django Docs 中阅读有关块和模板继承的更多信息

回答by oljik

I just wanted to add differences of extend and include. Both template and include can use models inserted in current app. Template is for global usage by your any app. Include is for use in certain apps. For ex: you want to insert Image Slider to your homepage and about page but nowhere else. You can create Slider app with its own model for convenience and import its model and include in that pages. If you used template for this example, you would create 2 templates one with slider and everything else other template have.

我只是想添加扩展和包含的差异。模板和包含都可以使用插入到当前应用程序中的模型。模板供您的任何应用程序全局使用。包含用于某些应用程序。例如:您想将 Image Slider 插入您的主页和关于页面,但没有其他地方。为方便起见,您可以使用自己的模型创建 Slider 应用程序,然后导入其模型并包含在该页面中。如果您在此示例中使用模板,您将创建 2 个模板,一个带有滑块,其他模板具有其他所有内容。

回答by Akshat Tamrakar

There are mainly 2 ways (2 easy ones)

主要有2种方式(2个简单的)

1:

1:

In base html put {% include "myapp/sub.html" %}

在基本 html 中放入 {% include "myapp/sub.html" %}

And just write html code inside your sub.html file

只需在 sub.html 文件中编写 html 代码

2:

2:

https://docs.djangoproject.com/en/dev/ref/templates/language/#template-inheritance

https://docs.djangoproject.com/en/dev/ref/templates/language/#template-inheritance