Django模板继承:要渲染多少个级别和要显示的页面 [英] Django Template inheritance: how many levels and what page to render

查看:88
本文介绍了Django模板继承:要渲染多少个级别和要显示的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在构建django模板级别时获得一些建议。

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

阅读文档,我不知道如何创建包含2个以上模板的层次结构

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

例如,我想创建3个继承级别:

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

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

这是我的代码:


  • base.html

  • base.html

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


  • base_level2.html

  • base_level2.html

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


  • level2_level3_1.html

  • level2_level3_1.html

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


  • level2_level3_2.html

  • level2_level3_2.html

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


  • views.py:

  • 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
    

    这对我来说是合乎逻辑的,因为我打电话给 render_to响应仅在level2_level3_1.html上。

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

    当然,如果调用level2_level3_2.html,我会收到消息第三层,第2部分,而不是第三级,第1部分

    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.

    推荐答案

    很难不知道它是个好主意还是不知道模板的特定功能,但是我的直接反应是您正试图过度组织模板。我认为大多数人会敦促您不要使用三层系统,因为它会使在网站上进行小的更改变得更加困难,并且更难以跟踪事物的位置。来自Python的禅宗:

    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

    两个Scoops of Django 中对三层系统的建议如下:

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



    1. 每个应用都有一个 base_< app_name> .html 模板。应用程序级基本模板共享一个公共父级base.html。

    2. 应用程序内的模板共享一个公共父级base _ < app_name> .html模板。

    3. 与base.html处于同一级别的任何模板都继承了base.html

    1. Each app has a base_<app_name>.html template. App-level base templates share a common parent, base.html.
    2. Templates within apps share a common parent base_<app_name>.html template.
    3. 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
    

    编辑:并没有真正的理由:

    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%}
    

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

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

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

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆