【问题标题】:Python: How can I implement this in a DRY way using Jinja2?Python:如何使用 Jinja2 以 DRY 方式实现这一点?
【发布时间】:2011-01-29 10:10:22
【问题描述】:

我的模板中有很多这样的代码:

  <h2>Owners of old cars</h2>
    <table id="hor-minimalist-b" summary="Old Cars">
    <thead>
        <tr>
            <th scope="col">Name</th>
            <th scope="col">Age</th>
            <th scope="col">Address</th>
        </tr>
    </thead>
    <tbody>
    {% for result in old_cars %}
        <tr>
            <td>{{result.name}}</td>
            <td>{{result.age}}</td>
            <td>{{result.address}}</td>
        </tr>
    {% endfor %}
    </tbody>
    </table>      

    <h2>Owners of Ford cars</h2>
    <table id="hor-minimalist-b" summary="Old Cars">
    <thead>
        <tr>
            <th scope="col">Name</th>
            <th scope="col">Age</th>
            <th scope="col">Address</th>
        </tr>
    </thead>
    <tbody>
    {% for result in ford_cars %}
        <tr>
            <td>{{result.name}}</td>
            <td>{{result.age}}</td>
            <td>{{result.address}}</td>
        </tr>
    {% endfor %}
    </tbody>
    </table>

将创建更多类似上面的表格。如您所见,有很多重复的代码。无论如何要这样做,所以每次创建表时我都不会重复太多代码?谢谢。

更新

我正在考虑创建一个名为 table 的新对象,并为其添加一个结果和相应的 h2 标题。然后我可以创建这些表对象的列表,称为表,我可以将其传递给模板。然后模板可以遍历它们。

【问题讨论】:

    标签: python dry jinja2


    【解决方案1】:

    当然,理想情况下,您想要的是:

    {% for car in cars %}
    <h2>Owners of {{ car.manufacturer }}</h2>
    <table id="hor-minimalist-b" summary="Old Cars">
    <thead>
        <tr>
            <th scope="col">Name</th>
            <th scope="col">Age</th>
            <th scope="col">Address</th>
        </tr>
    </thead>
    <tbody>
        {% for model in car.models %}
        <tr>
            <td>{{model.name}}</td>
            <td>{{model.age}}</td>
            <td>{{model.address}}</td>
        </tr>
        {% endfor %}
    </tbody>
    </table>
    {% endfor %}
    

    我不确定您使用的是什么 ORM,但在 Django 中构建起来并不难;毕竟,你传入的只是一个包含子对象和字段的对象列表。

    再一次,我只能从 django 的角度谈谈(从你自己的 ORM 构建),但你可以故意为每种类型的制造商构建带有明确请求的字典,或者更确切地说,有一个“销售”模型和一个模型为“制造商”,并在两者之间构建多对多的关系。在 django python 中,您的查询大致如下所示:

    result = []
    manufacturers = Manufacturer.objects.all() # get all manuf
    for m in manufacturer:
        mf = dict()
        mf["manufacturer"] = m.name
        mf["models"] = m.model_set.all() # get all linked models
        result.append(mf)
    # pass result to template as cars
    

    这有意义吗?

    【讨论】:

      【解决方案2】:

      使用模板作为表格的页眉和页脚

      <table id="hor-minimalist-b" summary="Old Cars">
      <thead>
          <tr>
              <th scope="col">Name</th>
              <th scope="col">Age</th>
              <th scope="col">Address</th>
          </tr>
      </thead>
      <tbody>
      

      将其放入名为“table_header.html”的文件中,并将其包含在您的模板中。 页脚也是如此!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-08-30
        • 2020-01-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-21
        • 1970-01-01
        相关资源
        最近更新 更多