简体   繁体   中英

Getting an empty query set Django

I'm trying to develop a search functionality but only getting a empty query set every single time .

class SearchView(TemplateView):
    template_name = "search.html"

    def get_context(self, **kwargs):
        context = super().get_context(**kwargs)
        kw = self.request.GET.get("search")
        results = Thread.objects.filter(Q(heading__icontains=kw) | Q(thread_content__icontains=kw))
        print(results)
        
        context["results"] = results
        return context

Template

{% extends 'base.html' %}

{% block title %}Search{% endblock %}

{% block content %}
<div class = "container">
    <div class = "row">
        <div class = "col-md-12">
            <h3>Search results for <span class="text-info">"{{ request.GET.search }}"</span></h3>
            <h3>{{results}}</h3>
            <hr>
            {% for item in results %}
            <div class="col-md-4">
                <img src = "{{item.image.url}}" class="img-fluid" alt = "">
               
            </div>
            <div class="col-md-8">
                <h4>{{item.heading}}</h4>
                <p>{{item.thread_content}}</p>

            </div>
            {%endfor%}
        </div>
    </div>
</div>

{% endblock %}

request.GET.search is returning correctly , but the rest is not getting displayed

This was a real strange issue . The Browser cache took a long time to refresh with my updated code, once I cleared the Browser Cache and Settings it started working again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM