小编Pyt*_*Doe的帖子

从 django 模板语言生成动态 HTML 表

我正在尝试使用django 模板语言生成动态 html 表,但我还无法做到。

这是有关我的Views.pytable.html的一些信息

视图.py

Class table(TemplateView):
      template_name = 'table.html'

      def get(self, request):
             header = {'header':['#', 'chemblID','Preferable Name']}
             rows = {'rows':{
                            'id':[1,2,3],
                            'chemblid':[534988,31290, 98765], 
                            'prefName':['A', 'B', 'C']}}

             return render(request,self.template_name, header,rows)
Run Code Online (Sandbox Code Playgroud)

(数据是硬编码的,因为我仍在测试。但是它应该根据用户输入进行更改。)

表.html

<table class="table">
    <thead>
        <tr>
            {% for k in header %}
            <th>{{k}}</th>
            {% endfor %}
        </tr>
    </thead>
    <tbody>
        {% for r in rows %}
            <tr>
                {% for e in r %}
                    <td>{{e.id}}</td>
                    <td>{{e.chemblid}}</td>
                    <td>{{e.prefName}}</td>
                {% endfor %}
            </tr>
        {% endfor %}
    </tbody> …
Run Code Online (Sandbox Code Playgroud)

django html-table django-templates python-3.x

3
推荐指数
1
解决办法
1万
查看次数

标签 统计

django ×1

django-templates ×1

html-table ×1

python-3.x ×1