使用django-autocomplete-light v3自定义项目的输出 [英] Customising the output of items using django-autocomplete-light v3
问题描述
在早期版本的django-autocomplete-light中,您可以使用模板来呈现每个返回的条目,其中包括插入自定义HTML的功能
我不能弄清楚如何使用常规API,所以我试图添加它。
到目前为止,我有一个类,这样使用 mark_safe
,HTML正在传递:
class TemplateRenderSelect2QuerySetView(autocomplete.Select2QuerySetView):
def get_result_label(self,result):
返回结果的标签。
template = get_template(autocomplete_light / item.html)
context = Context({item:result})
return mark_safe(template.render(context))
模板 autocomplete_light / item.html
是:
code>< b> {{item.name}}< / b>
但是,这些呈现为:
但是JSON是正确的标签:
{pagination:{more:false},results:[{text:< b> Victoria< / b>,id 11}]}
如何让django-admin正确呈现HTML?
编辑:我发现一些额外的文档,并尝试设置 attrs = {'data -html':'true'}
反对该小部件,但它仍然不工作
总是答案是子类化,在这种情况下,提示是t o覆盖 get_result_title
:
class CustomisableAutocomplete(autocomplete.Select2QuerySetView):
/ pre>
template_name =autocomplete_light / item.html
def get_result_title(self,result):
返回结果的标签。
template = get_template(self.template_name)
context = Context({result:result})
返回template.render(上下文)
如果你想拥有一个不含标签的标题,你可以覆盖
get_results
和返回更多数据:class CustomisableAutocomplete(autocomplete.Select2QuerySetView):
template_name =autocomplete_light / item.html
def get_result_title(self,result):
返回结果的标题
return six.text_type(result)
def get_result_text自我,结果):
返回结果的标签。
template = get_template(self.template_name)
context = Context({result:result})
return template.render(context)
def get_results自我,上下文)
返回响应的结果键的数据。
return [
{
'id':self.get_result_value结果)
'title':self.get_result_title(result),
'text':self.get_result_text(result),
}为上下文['object_list']
]
In earlier versions of django-autocomplete-light you could use a template to render each returned entry, which included the ability to insert custom HTML
I can't figure out how to do that using the regular API, so I'm trying to add it in.
So far I have a class like this which uses
mark_safe
and the HTML is being passed through: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))
And the template
autocomplete_light/item.html
is:<b>{{ item.name }}</b>
But thats being rendered as:
But the JSON is correct with the right tags:
{"pagination": {"more": false}, "results": [{"text": "<b>Victoria</b>", "id": 11}]}
How can I get django-admin to render the HTML properly?
edit: I found some extra documentation on custom HTML and tried setting
attrs={'data-html': 'true'}
against the widget, but its still not working解决方案As always the answer is subclassing, in this case the tip is to override
get_result_title
:class CustomisableAutocomplete(autocomplete.Select2QuerySetView): template_name = "autocomplete_light/item.html" def get_result_title(self, result): """Return the label of a result.""" template = get_template(self.template_name) context = Context({"result": result}) return template.render(context)
If you want to have a title that isn't cluttered with tags, you can override
get_results
and return more data:class CustomisableAutocomplete(autocomplete.Select2QuerySetView): template_name = "autocomplete_light/item.html" def get_result_title(self, result): """Return the title of a result.""" return six.text_type(result) def get_result_text(self, result): """Return the label of a result.""" template = get_template(self.template_name) context = Context({"result": result}) return template.render(context) def get_results(self, context): """Return data for the 'results' key of the response.""" return [ { 'id': self.get_result_value(result), 'title': self.get_result_title(result), 'text': self.get_result_text(result), } for result in context['object_list'] ]
这篇关于使用django-autocomplete-light v3自定义项目的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!