打开 html 文件并通过迭代向其中添加数据

open html file and add data into it with iteration

我有一个 html 发票文件。我想显示我的数据库中的一些数据,这些数据可以在 table.

的多行中

我如何使用迭代来显示我的发票中的数据。

例如:在一个组织中,有 3 个人使用优惠券进行付款。以及何时发票会生成所有三个人的姓名以及我要显示的日期和金额。

          <thead>
            <tr>
              <th class="col-4 border-0"><strong>Sites</strong></th>
              <th class="col-4 border-0"><span><strong>Mode</strong></span></th>
              <th class="col-4 border-0"><span><strong>Amount</strong></span></th>
            </tr>
          </thead>
          <tbody>
          <tbody>
          ---for loop here---
            <tr>
              <td class="col-4 text-1 border-0"> {var 1} </td>
              <td class="col-4 border-0"><span> {var 2} </span></td>
              <td class="col-4 border-0"><span> {var 3} </span></td>
            </tr>

          </tbody>
        </table>

我从视图传递数据的方式是:

    my_dict=[{'var1':'my site', 'var2': 'DIY', 'var3':'111'},
             {'var1':'my site2', 'var2': 'DIY', 'var3':'222'}]
    with open(BACKEND_TEMPLATE + 'Invoice_base.html') as inf:
        txt = inf.read()
        val = txt.format(date=today, invoice_id=str(identity), domain=FRONTEND_DOMAIN,
                         customer_name=(obj.customer.first_name + " " + obj.customer.last_name),
                         company=obj.customer.company_name, contact=obj.customer.phone_number, my_dict=my_dict)

我怎样才能做到这一点。

html 文件不支持模板标签,或者可能是我没有正确使用它。 如果有简单的解决方法请帮帮我!

我认为你在 html 中拼写错误 {var 1} 应该没有 space {var1} 那么它应该可以工作。


尽管使用 django 我推荐以下内容:

django Template 对象中有一个名为 Template.render()

的方法
  1. 所以在 html 文件中将所有 {var1} 更改为 {{var1}}
  2. 在您看来 my_dict"data": [list of var dicts]
  3. 使用 template = django.template.loader.gettemplate('path/to/template)
  4. 获取模板
  5. 使用 template.render(context=my_dict)
  6. 从 html 获取您的文本字符串

您可能需要根据需要稍微修改一下 for 循环。

我要感谢@CyDevos,我能够使用 BeautifulSoup 实现我想要的。 以下是我的做法。

  1. 将 BeautifulSoup 包导入为 Soup。
  2. 打开 TemplateCreated 一个带有 html 数据的 Soup 实例,在我的例子中它是-val
    with open(BACKEND_TEMPLATE + 'Invoice_base.html') as inf:
        val = inf.read()

    html_data = Soup(val, features="html5")
  1. 找到我们要更改的标签或 class。
    table_tbody = html_data.find('tbody')

  1. 添加您的新标签、文本、classes 或您想要的任何内容。
    for ele in invoice_ele_list:
        amount = str(ele['amount'])
        # print(amount, type(amount))
        new_row = html_data.new_tag('tr')
        td_site = html_data.new_tag('td')
        td_site.string = ele['site__slug']
        td_mode = html_data.new_tag('td')
        td_amount = html_data.new_tag('td')
        td_site['class'] = "col-4 text-1 border-0"
        td_mode['class'] = td_amount['class'] = "col-4 border-0"
        span_mode = html_data.new_tag('span')
        span_mode.string = ele['payment_for_permit_mode']
        span_amount = html_data.new_tag('span')
        span_amount.string = amount
        td_mode.insert(1, span_mode)
        td_amount.insert(1, span_amount)
        new_row.insert(1, td_site)
        new_row.insert(2, td_mode)
        new_row.insert(3, td_amount)
        table_tbody.insert(3, new_row)

有关详细信息,我会参考 BeautifulSoup 的文档。