title: How to retrieve data from database with autocomplete (Django)
body: <p>I need to get data from database and put it on input field but I can't do it.</p>
  
  <p>This is my code.</p>
  
  <p>template.html</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
      $(function () {
          $("#tags").autocomplete({
              source: "/autocomplete/",
              minLength: 2,
          });
      });
  &lt;/script&gt;
  
  &lt;div class="ui-widget"&gt;
      &lt;input id="tags"&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>urls.py</p>
  
  <pre><code>path('autocomplete', views.autocomplete, name='autocomplete'),
  </code></pre>
  
  <p>views.py</p>
  
  <pre><code>def autocomplete(self, request, *args, **kwargs):
      data = request.GET
      product = data.get("term")
      if product:
          products = Product.objects.filter(product_name__icontains=product)
      else:
          products = Product.objects.all()
  
      results = []
  
      for pd in products:
          pd_json = {}
          pd_json['id'] = pd.id
          pd_json['label'] = pd.product_name
          pd_json['value'] = pd.product_name
          results.append(pd_json)
      data = json.dumps(results)
      mimetype = 'application/json'
      return HttpResponse(data, mimetype)
  </code></pre>
  
  <p>models.py</p>
  
  <pre><code>class Product(models.Model):
      product_name = models.CharField(max_length=255)
      def __str__(self):
          return self.product_name
  </code></pre>
  
  <p>Or maybe because I didn't import something? (such as something about 'json')</p>