Django显示搜索结果

时间:2018-06-16 16:51:29

标签: django templates search

我目前正在Django的博客应用程序上工作。作为所有博客应用,我需要一个搜索表单。因此,我编写了一个查看搜索结果的小视图和上下文处理器(以使搜索表单全局可用):

view.py:

class BlogSearchListView(ListView):
    model = Post
    paginate_by = 10
    def get_queryset(self):
        qs = Post.objects.published()
        keywords = self.request.GET.get('q')
        if keywords:
            query = SearchQuery(keywords)
            title_vector = SearchVector('title', weight='A')
            content_vector = SearchVector('content', weight='B')
            tag_vector = SearchVector('tag', weight='C')
            vectors = title_vector + content_vector + tag_vector
            qs = qs.annotate(search=vectors).filter(search=query)
            qs = qs.annotate(rank=SearchRank(vectors, query)).order_by('-rank')
        return qs

base.html文件:

    <div class="globalsearch">
        <form id="searchform" action="{% url 'search' %}" method="get" accept-charset="utf-8">
            <label for="{{ categorysearch_form.category.id_for_label }}">In category: </label>
            {{ categorysearch_form.category }}
            <input class="searchfield" id="searchbox" name="q" type="text" placeholder="Search for ...">
            <button class="searchbutton" type="submit">
                <i class="fa fa-search"></i>
            </button>
        </form>
    </div>

settings.py:

TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.auth',
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
'quickblog.quickblog.context_processors.categorysearch_form',
)

context_processors.py

from .forms import PostForm

def categorysearch_form(request):
    form = PostForm()
    return {'categorysearch_form': form}

post_list.html:

{% extends 'quickblog/base.html' %}

{% block content %}
    {% for post in posts %}
        <div class="post">
            <h1><u><a href="{% url 'post_detail' pk=post.pk %}">{{ post.title }}</a></u></h1>
            <p>{{ post.content|linebreaksbr }}</p>
            <div class="date">
                <a>Published by: {{ post.author }}</a><br>
                <a>Published at: {{ post.published_date }}</a><br>
                <a>Category: {{ post.category }}</a><br>
                <a>Tag(s): {{ post.tag }}</a>
            </div>
        </div>
    {% endfor %}

    <div>
        <span>
         {% if posts.has_previous %}
            <a href="?page=1">&laquo; First <a> |</a></a>
            <a href="?page={{ posts.previous_page_number }}">Previous</a>
        {% endif %}
        {% if posts.has_next %}
            <span> Page {{ posts.number }} of {{ posts.paginator.num_pages }}.</span>
            <a href="?page={{ posts.next_page_number }}">Next<a> |</a></a>
            <a href="?page={{ posts.paginator.num_pages }}">Last &raquo;</a>
        {% endif %}
       </span>
   </div>
{% endblock %}

可以在此处查看项目的其余部分:https://github.com/rsmvdl/quickblog

我现在希望以与在post_list.html视图中显示的结果相同的方式呈现结果,其中包含一个细节:我希望用户搜索的单词在搜索结果中突出显示,以便用户能够尽快获得内容的上下文;)。我希望得到任何创意。感谢。

1 个答案:

答案 0 :(得分:0)

您可以创建custom template filter,在帖子标题或内容的搜索字词周围添加HTML标记。这不是一个确切的解决方案,但你应该能够适应它:

@register.filter
@stringfilter
def highlight_search_term(text, search_term):
    if search_term in text:
        text.replace(search_term, '<span class="highlight">' + search_term + '</span>')
    return mark_safe(text)

然后,您可以使用{{ post.title|highlight_search_term:search_term }}将此过滤器应用于模板中的帖子标题或内容。

您还需要将search_term传递给模板上下文,以便过滤器可以知道要突出显示的内容。如果您将此应用于用户提交的内容,请注意mark_safe()