title: Adding search bar function into a django project
body: <p>I'm trying to add search bar in my application but I don't know how to query a database to gives the things that user's search for. I want when user search for a <strong>user</strong> in a post or <strong>category</strong> in a post of <strong>model</strong> to shows the result that user search for, like YouTube search and facebook search, How can i do this in django to give me what i want ?</p>
  <p>this is my model:</p>
  <pre><code>class Photo(models.Model):
      user = models.ForeignKey(User, on_delete=models.CASCADE)
      category = models.CharField(max_length=30,null=True, blank=False)
      image = CloudinaryField(blank=False, null=False)
      description = models.TextField(null=True)
      date_added = models.DateTimeField(auto_now_add=True)
      phone = models.CharField(max_length=12, null=False, blank=False)
      price = models.CharField(max_length=30,blank=False)
      location = models.CharField(max_length=20, blank=False)
  
      def __str__(self):
          return str(self.category)
  </code></pre>
  <p>my search form in dashboard template:</p>
  <pre><code>  &lt;div class=&quot;container&quot;&gt;
     &lt;div class=&quot;row justify-content-center&quot;&gt;
       &lt;form action=&quot;{% url 'search' %}&quot; method=&quot;get&quot;&gt;
        &lt;input class=&quot;form-control me-2&quot; type=&quot;search&quot; placeholder=&quot;Search&quot; aria- 
        label=&quot;Search&quot;&gt;
        &lt;br&gt;
        &lt;button class=&quot;btn btn-outline-success&quot; type=&quot;submit&quot;&gt;Search&lt;/button&gt;
       &lt;/form&gt;
    &lt;/div&gt;
   &lt;/div&gt;
  </code></pre>
  <p>the post card in dashboard template:</p>
  <pre><code>&lt;div class=&quot;container&quot;&gt;
        &lt;div class=&quot;row justify-content-center&quot;&gt;
             {% for photo in photos reversed %}
                      &lt;div class=&quot;col-md-4&quot;&gt;  
                          &lt;div class=&quot;card my-2&quot;&gt;
                            &lt;img class=&quot;image-thumbail&quot; src=&quot;{{photo.image.url}}&quot; alt=&quot;Card image cap&quot;&gt;
                         
                            &lt;div class=&quot;card-body&quot;&gt;
                                 &lt;h2 style=&quot;color: yellowgreen; font-family: Arial, Helvetica, sans-serif;&quot;&gt;
                                 {{photo.user.username.upper}}
                                 &lt;/h2&gt;
                            &lt;br&gt;
                            &lt;h3&gt;{{photo.category}}&lt;/h3&gt;
                            &lt;h4&gt;{{photo.price}}&lt;/h4&gt;  
                           &lt;/div&gt;
                           &lt;a href=&quot;{% url 'Photo-view' photo.id %}&quot; class=&quot;btn btn-warning 
                            btn-sm m-1&quot;&gt;Buy Now&lt;/a&gt;
                         &lt;/div&gt;
                   &lt;/div&gt;
                   {% empty %}
                   &lt;h3&gt;No Files...&lt;/h3&gt;
                   {% endfor %} 
                  &lt;/div&gt; 
          &lt;/div&gt;
  </code></pre>
  <p>the dashboard view:</p>
  <pre><code>def dashboard(request):
      photos = Photo.objects.all()
      context = {'photos': photos}
      return render(request, 'dashboard.html', {'photos': photos} ) 
  </code></pre>
  <p>the search bar view:</p>
  <pre><code>def search(request):
  
      return render(request, 'search.html')
  </code></pre>
  <p>urls:</p>
  <pre><code>path('', views.dashboard, name='dashboard'),
  path('search/', views.search, name='search')
  </code></pre>