问题
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
- turn off escaping
OR - 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