本文介绍了如何在Django中加入其他应用程式的动态范本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有两个应用程序:

I currently have two apps:

app1/
app2/
templates/
    app1.html
    app2.html

在app1.html中,我包含了app2 .html:

In app1.html, I'm including app2.html:

<!-- app1.html -->
{% include "app2.html" %}

app2有一些动态内容: / p>

app2 has some dynamic content:

<!-- app2.html -->
{% app2_value %}

当我显示app1.html时,值 app2_value 不显示。在Django中最好的处理方法是什么?

When I display app1.html, the value app2_value doesn't show up. What's the best way to handle the above in Django?

推荐答案

Django并没有真正处理动态,包括PHP或其他语言。相反,你应该有一个基础模板,并使用模板继承和块来完成你想要做的。

Django doesn't really process dynamic including like PHP or other languages do. Instead, you should have a base template, and use template inheritance and blocks to accomplish what you're trying to do.

所以你的app2.html会有相同的动态内容,但是有一个app1.html可以覆盖或插入东西的地方。

So your app2.html would have the same dynamic content, but have a place for app1.html to either override or insert things.

app2.html:

{% block 'title' %}
{{ app2.title }}
{% endblock %}

{% block 'content' %}
{% endblock %}

App1的模板可以扩展App2的:

App1's template can then extend App2's:

app1.html:

{% extends "app2.html" %}

{% block 'title' %}
Actually App 1!
{% endblock %}

{block 'content' %}
...
{% endblock %}

不幸的是,包含处理在Django中仍然是新的,并且与我在文档和社区中看到的最佳做法相比。

Unfortunately, include-handling is still new in Django and against best practices from what I've seen in the documentation and community.

这篇关于如何在Django中加入其他应用程式的动态范本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 18:07