【问题标题】:Call function from HTML table data tags从 HTML 表格数据标签调用函数
【发布时间】:2014-11-10 18:06:55
【问题描述】:

以下是我的代码:

def format_taxes(self):
    tax_list = self.data['tax_list']
    send_tax = ''

    for tax in tax_list:
        send_tax = send_tax + unicode('<tr class="subtotal">'''
                                      '<td class="field">${label}</td>'''
                                      '<td class="total">${amount}</td>'''
                                      '</tr>').format(**tax)

    return send_tax

def subtotals_as_table(self):
        return unicode(u'<table>'
                            '<tr class="subtotal">'
                                '<td class="field">Subtotal</td>'
                                '<td class="total">${subtotal}</td>'
                            '</tr>'
                            '''
                            **How do I call the format_taxes() function in here?**
                            '''
                            '<tr class="subtotal">'
                                '<td class="field">Total</td>'
                                '<td class="total">${total}</td>'
                            '</tr>'
                        '</table>'.format(**self.data))

一切都很好。只是我想在标签数据中调用 format_taxes() 函数,我不知道该怎么做。谁能帮帮我?

【问题讨论】:

  • 你为什么要直接在 Python 中做这一切?您已将此问题标记为 django-templates,但您根本没有使用 Django 模板。它们正是为此而设计的,您应该使用它们。
  • @DanielRoseman:我还是个新手,还在努力学习。
  • 是的,所以你应该学习如何正确地做事。 Django 教程向您展示了如何使用模板系统:您应该先完成该教程。
  • 酷。会这样做。谢谢

标签: python html django django-templates


【解决方案1】:

要在 HTML 中调用,请使用以下代码:

【讨论】:

  • 不是OP需要调用的js函数。
  • 您确实需要更仔细地阅读问题。你的回答都没有真正解决问题。
【解决方案2】:

您不能在格式字符串中调用函数,但以正确的方式执行它并不难。很可能你想要的是这样的:

def subtotals_as_table(self):
    format_args = self.data.copy()
    format_args['taxes'] = self.format_taxes()
    return '<tr>...</tr> ${taxes} <tr>...</tr>'.format(**format_args)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-24
    • 2013-07-12
    • 1970-01-01
    • 1970-01-01
    • 2012-12-10
    • 2017-04-12
    • 2014-05-14
    • 1970-01-01
    相关资源
    最近更新 更多