Django - проблема с отображением объекта SearchHeadline в шаблоне
Я работаю над функциональностью поиска и мне нужно реализовать SearchHeadline для выделения искомых слов, но я затрудняюсь сделать это в нескольких полях.
Я хотел бы выделить не только слова в поле title, но и в поле short_description. Я нашел этот вопрос и попытался реализовать Concat
однако в моем случае title и short_description размещены в 2 отдельных html элементах, поэтому я не могу просто объединить их и отобразить как один заголовок, потому что мне нужно отобразить весь title и весь short_description и выделить совпадающие слова.
views.py
class SearchResultsList(LoginRequiredMixin, ListView, CategoryMixin):
model = QA
context_object_name = "query_list"
template_name = "search/SearchView.html"
def get_queryset(self):
query = self.request.GET.get("q")
search_vector = SearchVector("title", weight="A") + SearchVector("short_description", weight="B")
search_query = SearchQuery(query)
search_headline = SearchHeadline('title', search_query, start_sel='<span class="text-decoration-underline sidebar-color">',stop_sel='</span>') # how to implement SearchHeadline to short_description?
return QA.objects.annotate(
search=search_vector,
rank=SearchRank(search_vector, search_query)
).annotate(headline=search_headline).filter(rank__gte=0.3).order_by("-rank")
#The code below is working but it concatenates the title with the short description and I need to separate these fields. Moreover, it shows only a preview, not the whole title and short description value
# return QA.objects.annotate(search_vectors=SearchVector('title', 'short_description'), ).annotate(headline=SearchHeadline(Concat(F('title'), Value(' '), F('short_description')),SearchQuery(query),start_sel='<strong>', stop_sel='</strong>')).filter(search_vectors=SearchQuery(query))
SearchView.html
{% for query in query_list %}
<div class="col-12">
<a href="{{ query.get_absolute_url }}"><h2>{{query.headline | safe}}</h2></a>
<p>{{query.short_description |safe}}</p> # how to implement SearchHeadline to short_description so it displayed whole text and highlights matching words as in the title field?
</div>
{% empty %}
<div>No results</div>
{% endfor %}