Skip to content

Instantly share code, notes, and snippets.

@apjanco
Last active November 3, 2021 13:31
Show Gist options
  • Save apjanco/66f5a779779b2ae9ca3fbf7044a0f70f to your computer and use it in GitHub Desktop.
Save apjanco/66f5a779779b2ae9ca3fbf7044a0f70f to your computer and use it in GitHub Desktop.
corpus/snippets/document_result.html

I am currently working on issue 291 to highligh the search query in the search results. Search uses DocumentSearchView, which has a get_queryset method that returns the results and renders corpus/document_list.html

The result's description is line 27-28 in document_result.html

        {# description #}
        <p class="description">{{ document.description.0|truncatewords:25 }}</p>

Given that document.description is just a string, the simplest solution would be to add mark tags around the query in the description.

I would like to edit here: https://github.com/Princeton-CDH/geniza/blob/2ed07e07a8ae4b876d5e02c578f01263d2003cd5/geniza/corpus/views.py#L53

            search_opts = form.cleaned_data

            if search_opts["q"]:
                documents = documents.keyword_search(search_opts["q"]).also(
                    "score"
                )  # include relevance score in results

and add:

                #add mark to description 
                for doc in documents:
                    doc['description'][0] = doc['description'][0].replace(search_opts["q"],f'<mark>{search_opts["q"]}</mark>')
            

However, even with |safe in the template, Django is not showing the <mark>. In fact, any changes to description in get_queryset are not reflected on the page. What is the source of document.description.0 in document_result.html?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment