I have a data table in my Django project. This data table is for listing customers. The customer has attributes like name, country, email, etc… I want to put a button like a dropdown menu for listing countries of customers. (Excel-like or similar logic) But I just need this in the country column. How can I do that?
I try to use django-filters
but didn’t work.
customer_list.html
JavaScript
x
157
157
1
{% extends "layouts/base.html" %}
2
3
{% block title %} Customer List {% endblock %}
4
5
<!-- Specific Page CSS goes HERE -->
6
{% block stylesheets %}{% endblock stylesheets %}
7
8
{% block content %}
9
10
<div class="content">
11
<div class="page-inner">
12
<div class="page-header">
13
<div class="row">
14
<div class="col">
15
<h4 class="page-title">Customer List</h4>
16
</div>
17
18
<div class="col">
19
<a href="/customer">
20
<button class="btn btn-primary btn-round" style="">Add new customer</button>
21
</a>
22
</div>
23
</div>
24
25
</div>
26
27
<div class="row">
28
29
30
<div class="col-md-12">
31
<div class="card">
32
<div class="card-header">
33
<h4 class="card-title">My Customers</h4>
34
</div>
35
36
37
<div class="card-body">
38
<div class="table-responsive">
39
<form method="get">
40
{{ myFilter.form.as_p }}
41
<button class="btn btn-primary" type="submit">Filter</button>
42
</form>
43
<table id="multi-filter-select" class="display table table-striped table-hover grid_" >
44
<thead>
45
<tr>
46
<!-- class="filter" -->
47
<th index="0">Customer Name</th>
48
<th>Country</th>
49
<th>E-Mail</th>
50
<th>Phone</th>
51
<th>VAT Number</th>
52
<th>Operations</th>
53
</tr>
54
</thead>
55
<tfoot>
56
<tr>
57
<th>Customer Name</th>
58
<th>Country</th>
59
<th>E-Mail</th>
60
<th>Phone</th>
61
<th>VAT Number</th>
62
<th>Quick Operations</th>
63
</tr>
64
</tfoot>
65
<tbody>
66
{% for customer in customer_list %}
67
<tr>
68
<td>{{customer.customer_name}}</td>
69
<td>{{customer.country}}</td>
70
<td>{{customer.email}}</td>
71
<td>{{customer.telephone}}</td>
72
<td>{{customer.VATnumber}}</td>
73
<td>
74
<div class="row">
75
<a href="/customers/{{ customer.id }}/profile" class="btn btn-outline-primary btn-sm" data-toggle="tooltip" title="View Customer" ><i class="fas fa-eye"></i></a>
76
<a href="/customers/{{ customer.id }}/update" class="btn btn-outline-primary btn-sm" data-toggle="tooltip" title="Update Info" ><i class="fas fa-edit"></i></a>
77
<a href="/pdfs/{{ customer.id }}/list" class="btn btn-outline-primary btn-sm"><i class="fas fa-chart-line" data-toggle="tooltip" title="View Analyses" ></i></a>
78
</div>
79
</td>
80
</tr>
81
{% endfor %}
82
</tbody>
83
</table>
84
</div>
85
</div>
86
</div>
87
</div>
88
89
</div>
90
</div>
91
</div>
92
93
94
<style>
95
/* Styles go here */
96
97
table thead tr td{
98
background-color : gray;
99
min-width : 100px;
100
position: relative;
101
}
102
103
.filter{
104
position: relative;
105
border: solid 1px;
106
top : 20px;
107
background-color : red;
108
width:100px;
109
right:0;
110
}
111
</style>
112
113
114
{% endblock content %}
115
116
<!-- Specific Page JS goes HERE -->
117
{% block javascripts %}
118
119
120
<script src="/static/assets/js/setting-demo2.js"></script>
121
<script>$('#basic-datatables').DataTable();
122
123
$('#multi-filter-select').DataTable( {
124
"pageLength": 5,
125
initComplete: function () {
126
this.api().columns().every( function () {
127
var column = this;
128
var select = $('')
129
.appendTo( $(column.footer()).empty() )
130
.on( 'change', function () {
131
var val = $.fn.dataTable.util.escapeRegex(
132
$(this).val()
133
);
134
135
column
136
.search( val ? '^'+val+'$' : '', true, false )
137
.draw();
138
} );
139
140
column.data().unique().sort().each( function ( d, j ) {
141
select.append( ''+d+'' )
142
} );
143
} );
144
}
145
});
146
</script>
147
<script>
148
149
150
151
152
153
154
</script>
155
156
{% endblock javascripts %}
157
views.py
JavaScript
1
14
14
1
def customer_list(request):
2
current_user = request.user
3
userP = UserProfile.objects.get_or_create(username=current_user)
4
customer_list = Customer.objects.filter(company=userP[0].company.comp_name)
5
myFilter = TableFilter(request.GET, queryset=customer_list.all())
6
7
8
context = {
9
'customer_list': customer_list,
10
'myFilter': myFilter
11
12
}
13
return render(request, 'customer_list.html', context)
14
filters.py
JavaScript
1
9
1
import django_filters
2
from .models import *
3
4
5
class TableFilter(django_filters.FilterSet):
6
class Meta:
7
model = Customer
8
fields = 'country',
9
Advertisement
Answer
It’s apparent that the code that you have posted is only part of the code and so it’s not possible to see all the issues that may prevent it from working, part of the problem is that the fields parameter in your filters.py should have square brackets and no comma. It should read:
JavaScript
1
2
1
fields = ['country']
2