我是django的新手,请帮忙,我想在应用django过滤器搜索后获取字段的总和。 例如,我想获得后过滤器的总数。 第一次加载页面时,它会返回正确的输出,但是当我搜索并编号或记录更改时,它没有作用,仍然显示先前的输出。
*views.py*
```
from .forms import FormBuilding, FormUserCreation
from .filters import FilterBuilding, FilterFloor
from django.contrib.auth.forms import UserCreationForm
from django.contrib import messages
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import Group
from .decorators import unauthenticated_user, allowed_users, admin_only
@login_required(login_url='loginPage')
@allowed_users(allowed_roles=['admin'])
def building(request):
fltr_bld = FilterBuilding(request.GET, queryset=Building.objects.all())
apts=Building.objects.all().aggregate(Sum('total_apt')).get('total_apt__sum', 0)
return render(request, 'building.html', {'fltr_bld': fltr_bld, 'apts':apts})
```
*filter.py*
```
import django_filters
from django_filters import DateFilter, CharFilter
from .models import Building, Floor
class FilterBuilding(django_filters.FilterSet):
start_date = DateFilter(field_name='date_created', lookup_expr='gte')
end_date = DateFilter(field_name='date_created', lookup_expr='lte')
name = CharFilter(field_name='name', lookup_expr='icontains')
desc = CharFilter(field_name='desc', lookup_expr='icontains', label='Descriptions')
class Meta:
model = Building
fields = ['name', 'total_apt', 'desc', 'end_date', 'start_date']
exclude = ['other_details']
```
building.html
{% extends 'base.html'%}
{% load bootstrap4 %}
{% load widget_tweaks %}
{% block content %}
<br>
<br>
<div class="container">
<div class="row">
<div class="col">
<div class=" card card-body">
<form method="get">
<!--{{fltr_bld.form}}-->
{% bootstrap_form fltr_bld.form%}
<button type="submit" class="btn btn-outline-primary">Search</button>
</form>
</div>
</div>
</div>
<table class="table">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Total Apartment</th>
<th scope="col">Phone</th>
<th scope="col">Address</th>
<th scope="col">Date</th>
<th scope="col">Descriptions</th>
<th scope="col">Other Dtails</th>
<!--<th scope="col">Update</th>
<th scope="col">Delete</th>-->
</tr>
</thead>
<tbody>
{% for i in fltr_bld.qs%}
<tr>
<!--<td> <a class="btn btn-sm btn-outline-info" href="{% url 'building_floor' i.id %}">View</a></td>-->
<td>{{i.name}}</td>
<td>{{i.total_apt}}</td>
<td>{{i.phone}}</td>
<td>{{i.address}}</td>
<td>{{i.date_created}}</td>
<td>{{i.desc}}</td>
<td>{{i.other_details}}</td>
<!--<td><a class="btn btn-sm btn-outline-info" href={% url 'update_buidling' i.id %}>Update</a></td>
<td><a class="btn btn-sm btn-outline-danger" href={% url 'delete_buidling' i.id %}>Remove</a></td>-->
</tr>
{%endfor%}
</tbody>
</table>
<br>
<strong>Total Apartment: {{apts}}</strong>
</div>
<hr>
<!--<button type="submit" class="btn btn-outline-primary" style="float:right">Submit</button>-->
<!--<form class="container">
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp">
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1">
</div>
<div class="form-group form-check">
<input type="checkbox" class="form-check-input" id="exampleCheck1">
<label class="form-check-label" for="exampleCheck1">Check me out</label>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>-->
{% endblock%}