0

我正在使用django-ajax-datatable在我的 django 项目中呈现数据表

在这样做时,我想自定义一个称为操作的行呈现的示例如下

def customize_row(self, row, obj):
# 'row' is a dictionary representing the current row, and 'obj' is the current object.
row['code'] = '<a class="client-status client-status-%s" href="%s">%s</a>' % (
    obj.status,
    reverse('frontend:client-detail', args=(obj.id,)),
    obj.code
)
if obj.recipe is not None:
    row['recipe'] = obj.recipe.display_as_tile() + ' ' + str(obj.recipe)
return

注意这行代码

row['code'] = '<a class="client-status client-status-%s" href="%s">%s</a>' % (

如您所见,要呈现到行的 HTML 写在字符串中。对于我自己的具体情况,我想呈现以下 HTML,包括 django 模板标签(批量替换为 obj)

<a class="btn btn-sm btn-alt-secondary" href="{% url 'batch_detail' batch.pk %}" data-bs-toggle="tooltip" title="View">
     <i class="fa fa-fw fa-eye"></i>
</a>
{% if batch.approve == False %}
<a class="btn btn-sm btn-alt-secondary" href="{% url 'approve_manufacturing_batch' batch.pk %}" data-bs-toggle="tooltip" title="Approve">
     <i class="fa fa-fw fa-check-circle"></i>
</a>
{% else %}
<a class="btn btn-sm btn-alt-secondary" href="{% url 'approve_manufacturing_batch' batch.pk %}" data-bs-toggle="tooltip" title="Disapprove">
     <i class="si si-close"></i>
</a>
{% endif %}

我尝试了多种字符串格式化方法,但无法batch.approve在条件标签中定位

4

0 回答 0