【发布时间】:2016-12-08 13:26:51
【问题描述】:
在 django-autocomplete-light 的早期版本中,您可以使用模板来呈现每个返回的条目,其中包括插入自定义 HTML 的能力
我不知道如何使用常规 API 做到这一点,所以我正在尝试添加它。
到目前为止,我有一个这样的类,它使用mark_safe,并且正在传递 HTML:
class TemplateRenderSelect2QuerySetView(autocomplete.Select2QuerySetView):
def get_result_label(self, result):
"""Return the label of a result."""
template = get_template("autocomplete_light/item.html")
context = Context({"item": result})
return mark_safe(template.render(context))
而模板autocomplete_light/item.html是:
<b>{{ item.name }}</b>
但这被渲染为:
但是带有正确标签的 JSON 是正确的:
{"pagination": {"more": false}, "results": [{"text": "<b>Victoria</b>", "id": 11}]}
如何让 django-admin 正确呈现 HTML?
编辑:我在自定义 HTML 上找到了一些 extra documentation 并尝试针对小部件设置 attrs={'data-html': 'true'},但它仍然无法正常工作
【问题讨论】:
标签: jquery python django django-autocomplete-light