基于Django类的视图从表单查询数据库并显示结果

时间:2018-10-05 07:30:45

标签: django get django-views

因此,我对Django完全陌生,我希望用户以HTML形式输入关键字,然后在数据库中的每一行中,属性与页面上显示的关键字相匹配。我尝试了各种方法来执行此操作,但不确定自己在做什么错。任何帮助将不胜感激。

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'

1 个答案:

答案 0 :(得分:0)

您只需在get函数中执行Vehicles.objects.filter(makename__icontains=request.GET.get("make","somevalueasdefault"))。也许我缺少了一些东西,但是我不确定为什么要在基于类的视图中呈现这样的视图。仅作为示例,您可以执行以下操作。

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