Django在模板中显示搜索结果

时间:2018-06-17 12:03:19

标签: django templates search display

我对Django很新,我想在我的search.html模板中显示搜索结果,但我错过了路径... 我使用上下文处理器全局显示搜索框和类别选择器。因此,对数据库的查询是一组Catagorie和关键字。

  1. 不确定我是否以正确的方式实施。
  2. 不知道如何在我的模板中显示结果。
  3. 如何处理view.py中的catagoerie?
  4. base.py

    <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>
    

    views.py

    class globalsearch(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
    

    最后想要以我在我身上显示的方式显示结果 的 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 %}
    

    search.html

    {% extends 'quickblog/base.html' %}
    
    {% block content %}
        {% for post in object_list %}
            <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 %}
    
    {% if is_paginated %}
        <ul class="pagination">
            {% if page_obj.has_previous %}
                <li><a href="?page={{ page_obj.previous_page_number }}">&laquo;</a></li>
            {% else %}
                <li class="disabled"><span>&laquo;</span></li>
            {% endif %}
            {% for i in paginator.page_range %}
                {% if page_obj.number == i %}
                    <li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
                {% else %}
                    <li><a href="?page={{ i }}">{{ i }}</a></li>
                {% endif %}
            {% endfor %}
            {% if page_obj.has_next %}
                <li><a href="?page={{ page_obj.next_page_number }}">&raquo;</a></li>
            {% else %}
                <li class="disabled"><span>&raquo;</span></li>
            {% endif %}
        </ul>
    {% endif %}
    {% endblock %}
    

1 个答案:

答案 0 :(得分:1)

默认情况下,ListView会将您的查询集添加到上下文object_list,因此只需执行{% for obj in object_list %}来迭代post_list.html

中的内容

您的模板看起来像;

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

{% block content %}
    {% for post in object_list %}
        <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 %}

{% if is_paginated %}
  <ul class="pagination">
    {% if page_obj.has_previous %}
      <li><a href="?page={{ page_obj.previous_page_number }}">&laquo;</a></li>
    {% else %}
      <li class="disabled"><span>&laquo;</span></li>
    {% endif %}
    {% for i in paginator.page_range %}
      {% if page_obj.number == i %}
        <li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
      {% else %}
        <li><a href="?page={{ i }}">{{ i }}</a></li>
      {% endif %}
    {% endfor %}
    {% if page_obj.has_next %}
      <li><a href="?page={{ page_obj.next_page_number }}">&raquo;</a></li>
    {% else %}
      <li class="disabled"><span>&raquo;</span></li>
    {% endif %}
  </ul>
{% endif %}

您可以通过在视图中定义context_object_name来更改上下文变量名称。例如context_object_name = 'posts'