- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当使用 Django 模板时,我是否应该有一些像“子例程”一样的模板,或者在这些情况下我应该从我的代码中生成 HTML?
例如,我有一个包含多个名称列表的模板,我想将每个名称都变成select
。我是否应该有一个将 name_list
变量呈现到选择中的模板,并执行如下操作:
#in the view:
return {'name_list_1': name_list_1,
'name_list_2': name_list_2,
'name_list_3': name_list_3}
#in the template:
{% with name_list_1 as name_list %}
{% include "sub_name_list_select.html" %}
{% endwith %}
{% with name_list_2 as name_list %}
{% include "sub_name_list_select.html" %}
{% endwith %}
{% with name_list_3 as name_list %}
{% include "sub_name_list_select.html" %}
{% endwith %}
或者我应该在我的代码中添加一个函数,name_list_to_select_html
,它可以完成相同的工作,并执行以下操作:
return {'name_list_1_html': name_list_to_select_html(name_list_1),
'name_list_2_html': name_list_to_select_html(name_list_2),
'name_list_3_html': name_list_to_select_html(name_list_3)}
#in the template:
{{ name_list_1_html|safe }}
{{ name_list_2_html|safe }}
{{ name_list_3_html|safe }}
或者这两者都是错误的,而我的哲学完全错误?
补充问题:速度上来说,不断包含模板会不会很慢?这是代码内 html 生成的加分点吗?
最佳答案
一般来说,HTML 只能在模板系统或直接相关的代码中生成。这使得数据 View 与业务和功能逻辑完全分离。我觉得这是一个合适的separation of concerns 。采用您的第一个解决方案。
就性能而言,Django 应该花费大约相同的时间来运行任一代码。但是,如果您知道不需要在每个请求上重新生成这些代码段,那么它具有内置 View 和模板片段缓存。
关于python - Django哲学: when to include templates and when to have code generate html?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11566711/
C++ 核心指南(C++ Core Guidelines)是由 Bjarne Stroustrup、Herb Sutter 等顶尖 C+ 专家创建的一份 C++ 指南、规则及最佳实践。旨在帮助
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 4 年前。 Improve this qu
C++ 核心指南(C++ Core Guidelines)是由 Bjarne Stroustrup、Herb Sutter 等顶尖 C+ 专家创建的一份 C++ 指南、规则及最佳实践。旨在帮助
import React, { Component } from 'react' import _ from 'lodash' import { PageHeader, Row, Col, FormG
在《Scala 编程》第 239 页中,第一段说: Composition and inheritance are two ways to define a new class in terms of
我对 Node.js 比较陌生,我试图通过编写一个简单的模块来更熟悉它。该模块的目的是获取 ID、抓取网站并返回包含数据的字典数组。 网站上的数据分散在各个页面上,而每个页面都通过 URI 中的不同索
很确定我误解了 git。 我的目标 我在 github 上有一个带有“master”分支的私有(private)存储库。 我还想有一个生产分支,我会将所有更改从 master 推送到该分支。 然后我想
抱歉标题模糊; 我一直在重构我的一些 AngularJS 代码,试图让它更“有 Angular ”,我注意到这种模式经常出现: app.service("someService", function(
模拟 ScheduledExecutorService确实会让我的类(class)测试更容易,但根据 mockito recommendations这似乎是个坏主意,因为模拟类的逻辑可能会以不正确的方
我正在使用 LINQ to XML 生成 XML 文档文档。我希望 XML 文档最小化,即应该省略很少使用的属性。目前,我是这样做的: XElement element = new XElement(
这里是一个使用响应式(Reactive)编程的案例(例如使用 RxJava) 有一个具有一些属性(姓名、姓氏)的 User 对象 - 可观察 一个 Activity 包含 2 个 fragment ,
这不是一个非常精确的问题。我想我理解“ react 提升状态”范式。据我所知,这是两个同级组件访问各自属性的唯一干净方式。 但这样做,我最终得到了一个包含所有内容的巨大类:所有子组件属性的数据信息,以
我是一名优秀的程序员,十分优秀!