Django 模板继承:要渲染多少层和什么页面

编程入门 行业动态 更新时间:2024-10-24 04:47:38
本文介绍了Django 模板继承:要渲染多少层和什么页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在构建 django 模板级别方面提供一些建议.

阅读文档,我不明白如何创建超过 2 个级别的模板层次结构.

例如,我想创建 3 个层次的继承:

base.html└── base_level2.html├── level2_level3_1.html└── level2_level3_2.html

这是我的代码:

base.html

第一级{% block level2 %}{% endblock %}

base_level2.html

{% 扩展 "base.html" %}{% 块级别 2 %}第二级{% block level3_1 %}{% endblock %}{% block level3_2 %}{% endblock %}{% 结束块 %}

level2_level3_1.html

{% 扩展 "base_level2.html" %}{% 块 level3_1 %}第三级,第 1 部分{% 结束块 %}

level2_level3_2.html

{% 扩展 "base_level2.html" %}{% 块 level3_2 %}第三级,第 2 部分{% 结束块 %}

views.py:

def myView(request):return render_to_response('level2_level3_1.html', {}, context_instance=RequestContext(request))

这样我可以在浏览器上看到以下内容:

第一级第二级第三级,第 1 部分

这对我来说是合乎逻辑的,因为我只在 level2_level3_1.html 上调用 render_to response.

当然,如果调用 level2_level3_2.html,我会得到消息Third level, part 2 而不是 Third level, part1.

如何解决这个问题?这是一个好方法吗?我以这种方式构建了东西,因为我的真实模板非常大,代码行很多,所以我想保持一些秩序.任何建议将不胜感激.

解决方案

在不知道模板的具体功能的情况下很难说这是一个好主意还是坏主意,但我的直接反应是你正在尝试过度组织您的模板.我认为大多数人会敦促您远离 3 层以上的系统,因为它会使对网站进行小的更改变得更加困难,并且更难以跟踪事物的位置.来自 Python 之禅:

<块引用>

扁平优于嵌套

两勺Django中对三层系统的推荐如下:

<块引用>

每个应用程序都有一个 base_.html 模板.应用级基本模板共享一个公共父级 base.html.应用内的模板共享一个共同的父基_.html 模板.任何与base.html同级的模板都继承base.html

对于您的命名架构,它可能如下所示:

<代码> |模板/|--base.html|--someothertemplate.html # 扩展 base.html|--level2/|----base_level2.html # 扩展 base.html|----level2_1.html # 扩展 base_level2.html|----level2_2.html # 扩展 base_level3.html

并没有真正的原因:

 二级{% block level3_1 %}{% endblock %}{% block level3_2 %}{% endblock %}

其中每个块引用一个模板的内容.您可以将其简化为一个块

{% block level3 %}{% endblock level3%}

然后在每个 level3 模板中,相应地重命名块

I would like to have some advice in constructing django template levels.

Reading the docs, I do not understand how to create a template hierarchy structure with more than 2 levels.

For example, I would like to create 3 levels of inheritance:

base.html
    └── base_level2.html
            ├── level2_level3_1.html
            └── level2_level3_2.html

This is my code:

base.html

First level
{% block level2 %}{% endblock %}

base_level2.html

{% extends "base.html" %}
{% block level2 %}
    Second level
    {% block level3_1 %}{% endblock %}
    {% block level3_2 %}{% endblock %}
{% endblock %}

level2_level3_1.html

{% extends "base_level2.html" %}
{% block level3_1 %}
    Third level, part 1
{% endblock %}

level2_level3_2.html

{% extends "base_level2.html" %}
{% block level3_2 %}
    Third level, part 2
{% endblock %}

views.py:

def myView(request):
    return render_to_response('level2_level3_1.html', {}, context_instance=RequestContext(request))

In this way I can see the following on my browser:

First level
Second level
Third level, part 1

And this is logical to me because I call render_to response only on level2_level3_1.html.

Of course, if call level2_level3_2.html, I get the message Third level, part 2 but not the Third level, part1.

How to solve this? Is that a good approach? I've structured stuff in this way because my real templates are very big, a lot of lines of code, so I would like to keep some order. Any advice will be appreciated.

解决方案

It's hard to say if it's a good or bad idea or not without knowing the specific functionality of your templates, but my immediate reaction is that you're trying to over organize your templates. I think most people would urge you away from more than a 3-tier system because it makes it more difficult to make small changes in the website and more difficult to keep track of where things are. from the Zen of Python:

Flat is Better than Nested

The recommendation for a 3-tier system inTwo Scoops of Django goes like this:

Each app has a base_<app_name>.html template. App-level base templates share a common parent, base.html. Templates within apps share a common parent base_<app_name>.html template. Any template at the same level as base.html inherits base.html

and for your naming schema, it might look like this:

  | Templates/
  |--base.html
  |--someothertemplate.html # extends base.html
  |--level2/
  |----base_level2.html     # extends base.html
  |----level2_1.html        # extends base_level2.html
  |----level2_2.html        # extends base_level3.html

EDIT: and there's no real reason for this:

    Second level
   {% block level3_1 %}{% endblock %}
   {% block level3_2 %}{% endblock %}

where each block refers to the content of one template. you can simplify that to one block like

{% block level3 %}{% endblock level3%}

and then in each of the level3 templates, rename the blocks accordingly

这篇关于Django 模板继承:要渲染多少层和什么页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-03-27 10:27:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/686716.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模板   页面   Django

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!