【问题标题】:Django template integer value iterationDjango模板整数值迭代
【发布时间】:2020-01-02 01:11:48
【问题描述】:

我有以下型号

class Table(models.Model):
    # Some not important attrs
    rows = IntegerField(etc)
    cols = IntegerField(etc)

然后我就有了我在哪里渲染这个模型的对象的视图。我需要根据每个对象的行数和列数构建一些 HTML 表格。

查看:

get_tables(request):
    tables = Table.objects.all()
    return render(request, 'tables.html', {'tables': tables})

我正在尝试做类似的事情:

{% for table in tables %}
    <table>
        <thead>
            <tr>
                {% for col in table.cols %}
                    <th>column label here</th>
                {% endfor %}
            </tr>
        </thead>
        <tbody>
            {% for row in table.rows %}
                <tr>my row</tr>
            {% endfor %}
        </tbody>
    </table>
{% endfor %}

我知道可以循环输入字典中的键。但是值 cols 和 rows 是整数。如何在 Django 模板上实现这一点?

【问题讨论】:

    标签: django loops templates integer iteration


    【解决方案1】:

    试试

    {% for table in tables %}
        <table>
            <thead>
                <tr>
                    {% with ''|center:table.cols as range %}
                    {% for _ in range %}
                        <th>column label here</th>
                    {% endfor %}
                    {% endwith %}
                </tr>
            </thead>
            <tbody>
                {% with ''|center:table.rows as range %}
                {% for _ in range %}
                    <tr>my row</tr>
                {% endfor %}
                {% endwith %}
            </tbody>
        </table>
    {% endfor %}
    

    【讨论】:

      【解决方案2】:
      # You can take use of filter tags in django template
      
      # For Example
      
      Step 1:- Create 'templatetags' package in your app folder.
      
      Step 2:- Create filter.py in 'templatetags' package
      
      Step 3:- 
      
      from django import template
      
      register = template.Library()
      
      def table_rows(value):
          value_list = [value]
          html_row = ''
      
          for val in value_list:
              html_row += '<tr></tr>'
          return html_row
      
      def table_cols(value):
          value_list = [value]
          html_cols = ''
      
          for val in value_list:
              html_cols += '<td>Hello</td>'
          return html_cols
      
      
      register.filter('table_rows', table_rows)
      register.filter('table_cols', table_cols)
      
      
      Step 4:-
      
      # Your template can be:-
      
      {% load filter %}
      
      {% for table in tables %}
          <table border="1">
          {% autoescape off %}
          {{table.rows|table_rows}}
          {{table.cols|table_cols}}
          {% endautoescape %}
          </table>
      {% endfor %}
      
      # You can make changes according to your requirement
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-12-14
        • 1970-01-01
        • 2015-12-10
        • 2012-07-27
        • 1970-01-01
        • 1970-01-01
        • 2021-10-28
        相关资源
        最近更新 更多