Unescape search results with django haystack and elasticsearch

[亡魂溺海] 提交于 2020-01-06 02:15:32

问题


I'm using django-haystack with elasticsearch backend. The data contains names of books that may contain special characters like &, ' or "". The indexed data escapes these characters and the search results shows the escaped data. How do I tell haystack or elasticsearch to

  1. turn off escaping
    OR
  2. unescape the characters when I want to use the results in a non-HTML context i.e. as plain text ?

Here's my code:

#search_indexes.py
class Book(indexes.SearchIndex, indexes.Indexable):
    text = indexes.EdgeNgramField(document=True, use_template=True)

    def get_model(self):
        return Book

#template
{{object.name}}

#query
SearchQuerySet().autocomplete(text=my_query)

回答1:


In your template you can use filters and tags like:

{% autoescape on %}
    {{ object.name }}
{% endautoescape %}

or

{{ object.name|striptags }}


来源:https://stackoverflow.com/questions/26570873/unescape-search-results-with-django-haystack-and-elasticsearch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!