If statement not working when i perform a search

12 views
Skip to first unread message

Namanya Daniel

unread,
Mar 9, 2023, 4:23:09 AM3/9/23
to Django users
The following code has an if statement that is not working. 
In my views,  i attach 'Results' 
In the templates i try to check if there are no results, i display no results found or else display results 

Challenge is, the if statement is not working. I used {% if not results %}and {% if results %}on None, but in both cirumstances the if statement failed.

Kindly share how i can improve this, Thanks 


 <div class="row row-cols-1 row-cols-md-7 g-4 m-3 ">

    {% if not results%}
    <span class="content-justify-middle" type=" text-bold">
      <h1>No search results found!</h1>
    {% else %}
    {% for result in results%}
    <div class="p-2">
      <!--Card 1-->
      <div class=" md:flex md:flex-row h-auto p-4 rounded-lg shadow-md ">
        <img class="md:w-32 md:h-32 sm:wd-100% rounded-lg object-cover mr-4" src="/media/{{result.logo}}" alt="{{result.business_name}}">
        <!--One of the images is hidden on smaller devices -->
       
        <div class="">
          <div class="mb-4">
            <p class=" text-sm text-gray-600 flex items-center">
              {{result.category}}
              <span class = "text-sm text-teal-800 font-mono bg-teal-100 inline rounded-full px-2 align-top float-right animate-pulse" ><i class="fas fa-star-o mr-1 text-grey-dark"></i>{{result.average_rating|floatformat:2}}
              {{result.count}} {% if result.count > 1 %} Ratings {%else%} Rating {%endif%}</span>
            </p>
            <div class="text-grey-dark  font-bold text-xl mb-2"><a href="/{{result.category}}/{{result.listingid}}/{{result.business_name}}">{{result.business_name}}</a></div>
            <span class="text-gray-700 text-base"><i class="fas fa-map-marker-alt mr-1 text-grey-dark"></i> {{result.address}}, {{result.city}}, {{result.country}}</span>
            </p>
          </div>

Reply all
Reply to author
Forward
0 new messages