Search code examples
djangogetdjango-views

Django class based view to query database from form and display results


So I am completely new to Django, I want to have a user enter a keyword into an HTML form then have each row from the database where an attribute matches that keyword displayed on the page. I've tried various ways of doing this and am not sure what I am doing wrong. Any help would be appreciated.

search.html

<div class="container">
    <form method="GET" action="{% url 'search' %}">
        <div class="form-group">
            <input type="text" name="make" placeholder="Car Make" />
            <label>
                <button type="submit" class="btn btn-danger"> Go </button>
            </label>
        </div>
    </form>

{% if results %}
<table>
    <tr>
        <th scope="col"></th>
        <th scope="col">Car Make</th>
        <th scope="col">Car Model</th>
        <th scope="col">Car Type</th>
        <th scope="col">Number of Seats</th>
        <th scope="col">Price</th>
    </tr>
    {% for item in results%}
    <tr>
        <td>{{item.makename}}</td>
        <td>{{item.model}}</td>
        <td>{{item.seriesname}}</td>
        <td>{{item.seatingcapacity}}</td>
        <td>{{item.pricenew}}</td>
    </tr>
    {% endfor %}
</table>
{% endif %}

</div>

views.py

class SearchView(TemplateView):

    template_name = 'carproject/search.html'
    model = Vehicles

    def get(self, request):
        form = AdvancedSearch()
        return render(request, self.template_name, {'form': form})

    def search(self, request):
        makequery = self.request.GET.get['make']
        if makequery:
            results = self.Vehicles.objects.filter(makename__icontains(makequery))
        return render(request, self.template_name, {'results': results})

Models.py

class Vehicles(models.Model):
    carid = models.IntegerField(db_column='CarID', primary_key=True)
    makename = models.CharField(db_column='MakeName', max_length=45)
    model = models.CharField(db_column='Model', max_length=45)
    seriesname = models.CharField(db_column='SeriesName', max_length=45)
    seriesyear = models.TextField(db_column='SeriesYear') 
    pricenew = models.IntegerField(db_column='PriceNew')
    fuelsystem = models.CharField(db_column='FuelSystem', max_length=45)
    enginesize = models.CharField(db_column='EngineSize', max_length=10) 
    tankcapacity = models.CharField(db_column='TankCapacity', max_length=10)
    power = models.CharField(db_column='Power', max_length=10)
    seatingcapacity = models.IntegerField(db_column='SeatingCapacity')
    standardtransmission = models.CharField(db_column='StandardTransmission', max_length=45)
    bodytype = models.CharField(db_column='BodyType', max_length=45)
    drive = models.CharField(db_column='Drive', max_length=3)
    wheelbase = models.CharField(db_column='WheelBase', max_length=10)
    class Meta:
        managed = False
        db_table = 'vehicles'

Solution

  • You can just do Vehicles.objects.filter(makename__icontains=request.GET.get("make","somevalueasdefault")) in your get function. Maybe I am missing something, but I am not sure why you have rendered the view like that in a class-based view. Just as an example, you can do like below.

    class SearchView(TemplateView):
        template_name = "carproject/search.html"
    
        def get(self, kwargs):
          context = super(SearchView, self).get_context_data(**kwargs)
          context['queryset'] = Vehicles.objects.filter(makename__icontains=request.GET.get("make","sdefault"))
          return context