- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在后端使用 Flask 应用程序,它应该使用 Jinja2 模板内的循环在前端呈现 SKU(库存单位)代码列表。 SKU分类如下:
class SKU:
"""Class to hold SKU data returned from the database."""
def __init__(self, sku, scanned_at, actual_count, expected_count):
"""Initialize the SKU class with relevant attributes."""
self.sku = str(sku)
self.scanned_at = str(scanned_at)
self.actual_count = int(actual_count)
self.expected_count = int(expected_count)
def get_progress(self):
"""Get the SKU production progress as a percentage."""
return ((self.actual_count / self.expected_count) *
100 if self.expected_count != 0 else 0)
我有一个方法 get_all_skus_today()
返回数据库中今天日期的所有行,作为 SKU
对象的列表。当有人使用以下路由访问 /skus
时,我想呈现它:
@app.route("/skus")
def skus():
"""Get all SKUs for the day and render the skus.html template."""
skus = get_all_skus_today()
return render_template("skus.html", skus=skus)
问题是我要显示进度值,也就是函数get_progress()
的返回值,不是Class属性,而是方法。我想做这样的事情:
{% for sku_row in skus %}
{{ sku_row.sku }}
{{ sku_row.get_progress }}
{% endfor %}
但这行不通。我想避免循环遍历 SKU 对象列表并将它们转换为元组,然后传递给 render_template
函数(这是我之前所做的)。
非常感谢任何帮助 - 如果您需要任何进一步的说明,请告诉我。
最佳答案
您可以创建一个额外的类来加载和处理来自数据库的信息,并创建一个 Sku
对象列表:
import sqlite3
class _Sku:
def __init__(self, row):
self.__dict__ = dict(zip(row, ['_sku', 'scanned_at', 'actual_count', 'expected_count']))
@property
def sku(self):
return str(self._sku)
@property
def get_progress(self):
return ((int(self.actual_count) / int(self.expected_count)) *
100 if int(self.expected_count) != 0 else 0)
class Sku:
def __init__(self, _listing):
self.all_vals = [_Sku(i) for i in _listing]
def __iter__(self):
yield from self.all_vals
@classmethod
def create_skus(cls, _filename='somefilename.db'):
#if so desired, you can replace query below with your own
return cls(sqlite3.connect(_filename).cursor().execute("SELECT scanned, actual, expected FROM skus"))
然后,在应用程序中:
@app.route("/skus")
def skus():
"""Get all SKUs for the day and render the skus.html template."""
return render_template("skus.html", skus=Sku.create_skus())
现在,上面的代码将启用您的原始模板:
{% for sku_row in skus %}
{{ sku_row.sku }}
{{ sku_row.get_progress }}
{% endfor %}
关于python - 在 Jinja2 模板中访问类方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52594285/
这是我的代码: {% if {{post.author.profile.image.url}} is None %} 当我运行这段代码时出现错误 Could not parse the remaind
我想将 jinja 循环内的单个 jinja 变量作为参数传递给 python 函数。在这种情况下,我需要将 jinja 循环内的 {{ book.isbn }} 传递给 python 函数才能使用该
问题几乎在标题中。 IntelliJ 是否具有针对 .jinja 的 native 语法高亮显示?和 .j2文件? 如果没有,是否有插件? 最佳答案 这个插件:https://plugins.jetb
我只想循环现有列表并从中创建一个逗号分隔的字符串。 像这样的东西:my_string = 'stuff,stuff,stuff,stuff' 我已经了解loop.last,我只需要知道如何使下面代码中
我在 HTML 表格中添加了复选框,我想知道是否选中了任何复选框。我有一个在 Jquery 中运行的脚本,它告诉我变量中标记的复选框的数量,但我还没有找到如何在 Jinja if 语句条件中使用 Jq
嗨,我觉得我需要为我现在挣扎了几个小时的事情提供答案。我希望有一天这对某人有所帮助。 我一直在尝试自动化 Ansible 部署的模板: 库存内容: [splunk_license] 10.10.113
我想记录我的 Jinja 宏是如何工作的。在 Jinja 中是否有用于记录宏的官方格式,就像有多种格式化 Python 文档字符串的方法一样? 也许是这样的: {% macro my_macro(ar
有没有办法加载自定义标签集 - 甚至是单个自定义标签? 在 django 模板中,我可以这样做: {% load custom_tag_set %} 有没有办法在神社中做到这一点? 最佳答案 存在相同
我正在创建一种表单,用户可以在其中添加一个或两个额外的字段...我正在使用jQuery进行交互,并使用Flask进行模板化,它看起来像这样: Add College or University
所以我试图在 url_for 中嵌套 2 个变量来提取特定人物的照片。这是我的代码 我的图像采用“用户名.png”格式,因此我在开头和结尾添加“_”。 这是它的Python部分: @app.rout
我想做的基础是使用“随机”过滤器从我的列表中选择一个随机项目,但随后我想在多个位置使用该随机选择的项目。 如何将过滤器的结果设置为可以在多个位置使用的变量。 如果我多次调用“随机”过滤器,它们几乎不可
我想知道在文件中添加包含或宏时是否有任何方法可以保持 jinja 的缩进。我想使用 jinja 生成代码文件。一个例子是 文件:class.html class MyClass: def s
我已经能够使用 BaseOperator 中的函数 render_template 成功渲染 Jinja 模板。 我的问题是,有谁知道在Rendered或Rendered Template选项卡下将渲
我从以下 ninja2 模板开始,但如果第二列的值 > 指定的宽度,它只会打印整个字符串,其余列右移。 {{ "%-7s"|format(tbl[3]) }} {{ "%-12s"|form
我一直在尝试使用 Flask-Cache 的 memoize 功能来仅返回 statusTS() 的缓存结果,除非在另一个请求中满足特定条件,然后删除缓存。 但它并没有被删除,并且 Jinja 模板仍
在 jinja 文件中找到其中一些: {%- else -%} 还有 {% if freeswitch_dispatcher -%} 看到破折号了吗?知道它的用途吗? 最佳答案 结果是 + 和
我从以下 ninja2 模板开始,但如果第二列的值 > 指定的宽度,它只会打印整个字符串,其余列右移。 {{ "%-7s"|format(tbl[3]) }} {{ "%-12s"|form
好的,所以我有了第一个 HTML 文件(header.html): M4A {% load staticfiles %} {% block content %}
我有一个看起来像这样的Python字典 [{"hat": 91, "d1b": 2, "d1c": 1, "d1d": 5, "d1e": 7, "d1f": 77, "d1e": 999} {"ha
我正在使用 SaltStack 来管理 BIND9 区域文件。以前我使用过这样的支柱数据: zones: example.com: a: www1: 1.2.3.4
我是一名优秀的程序员,十分优秀!