我有一个项目,其中当用户输入车辆编号时,将过滤数据库,并显示一个包含该车辆编号和对应信息的表。我还想过滤此显示的表,例如:如果用户选择查看数量大于18kl的数量,则显示数量大于18的匹配车辆编号。我也想隐藏用户选择的列,因为有很多列。有人可以告诉我如何在Django中执行此操作,或提出一些更好的方法。 (我仅提供相关的代码段。)
class VehicleSearch(forms.Form):
vehicl[![enter image description here][1]][1]e_no = forms.CharField(widget=forms.TextInput(attrs={'class': 'special'}), required=False)
#filter form
class Filter(forms.Form):
capacity_grp = forms.ChoiceField(label='Show only', widget=forms.RadioSelect,
choices=[('abv', '>18 kl'), ('blw', '<18 kl')], required=False)
def search(request):
form_1 = forms.VehicleSearch()
if request.method == 'POST' and 'btnform1' in request.POST:
form_1 = forms.VehicleSearch(request.POST)
if form_1.is_valid():
vehicle_no = form_1.cleaned_data['vehicle_no']
transport = models.Transport.objects.filter(vehicle=vehicle_no)
my_dict.update({'transport': transport})
return render(request, 'search.html', my_dict)
/ 车辆形式 /
<form id="f1" method="POST">
{% csrf_token %}
{{form_1.as_p}}
<p style="padding: 10px;"><button class="myButton" name="btnform1">Search</button></p>
</form>
/*Table display*/
<div class="submain">
{% if transport %}
<table id="transportation">
<thead>
<th>Vehicle</th>
<th>Carrier</th>
<th>Location No</th>
<th>MCMU</th>
<th>Location</th>
<th>Customer Code</th>
<th>Zone</th>
<th>Quantity</th>
<th>RTKM</th>
<th>KL* KM</th>
<th>Amount</th>
<th>Load</th>
<th>Capacity</th>
<th>Rate</th>
<th>Cost</th>
</thead>
{% for i in transport %}
<tr class="item">
<td>{{ i.vehicle }}</td>
<td>{{ i.carrier }}</td>
<td>{{ i.location_no }}</td>
<td>{{ i.mcmu }}</td>
<td>{{ i.location }}</td>
<td>{{ i.customer_code }}</td>
<td>{{ i.zone }}</td>
<td>{{ i.quantity }}</td>
<td>{{ i.rtkm }}</td>
<td>{{ i.klkm }}</td>
<td>{{ i.amount }}</td>
<td>{{ i.load }}</td>
<td>{{ i.capacity }}</td>
<td>{{ i.rate }}</td>
<td>{{ i.cost }}</td>
</tr>
{% endfor %}
</table>
</div>
答案 0 :(得分:0)
在表格显示中,您可以添加带有名称字段的循环,如下所示:
查看:
def search(request):
form_1 = forms.VehicleSearch()
if request.method == 'POST' and 'btnform1' in request.POST:
form_1 = forms.VehicleSearch(request.POST)
columns = request.POST.getlist('filter_hide_columns')
if form_1.is_valid():
vehicle_no = form_1.cleaned_data['vehicle_no']
transport = models.Transport.objects.filter(vehicle=vehicle_no)
my_dict.update({'transport': transport}, {'columns': columns})
return render(request, 'search.html', my_dict)
TemplateTag
def lookup(model, attr):
if hasattr(model, attr):
return getattr(model, attr)
else:
return None
推荐https://docs.djangoproject.com/en/2.2/howto/custom-template-tags/
模板:
{% if transport %}
<table id="transportation">
<thead>
<tr>
{% for field in columns %}
<th>{{ field.name }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for t in transport %}
<tr>
{% for field in columns %}
<td>{{ t|lookup:field.name }}</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
请参阅https://docs.djangoproject.com/en/2.2/topics/forms/#looping-over-the-form-s-fields
答案 1 :(得分:0)
要搜索车辆编号,您可以这样做。
我假设您有一个Vehicle
的模型:
Models.py
class Vehicle(model.Model):
vehicle_no = models.IntegerField()
vehicle_weight = models.IntegerField()
search.html
<form action="{% url 'your_search_url' %}">
<input type="text" name="q" placeholder="Search">
</form>
在views.py
def search(request):
q = request.GET.get('q')
if q:
vechicles = Vehicle.object.filter(vehicle_no__contains=q)
return render(request,'your_display_table.html',{'vehicles':vehicles})
else:
messages.info(request,'no results found for {} ',format(q))
return redirect('your_view')
要过滤值大于18的数据;
def filter(request):
vechicles = Vehicle.object.filter(vehicle_weight__gt=18)
return render(request,'your_display_table.html',{'vehicles':vehicles})
在模板中,您可以这样使用:
{% for vehicle in vehicles %}
{{vehicle.name}}
{% endfor %}