pip install django-filter
1. Filters.py
from django.contrib.auth.models import User
import django_filters
class UserFilter(django_filters.FilterSet):
class Meta:
model = User
fields = ['username', 'first_name', 'last_name', ]
Set label and match type
class TranslatorFilter(django_filters.FilterSet):
profile__email = django_filters.CharFilter(label='Email', lookup_expr='exact',)
profile__first_name = django_filters.CharFilter(label='First name', lookup_expr='icontains',)
profile__last_name = django_filters.CharFilter(label='Last name', lookup_expr='icontains',)
class Meta:
model = Translator
fields = fields = {
}
2. Views.py
from django.contrib.auth.models import User
from django.shortcuts import render
from .filters import UserFilter
def search(request):
user_list = User.objects.all()
user_filter = UserFilter(request.GET, queryset=user_list)
return render(request, 'search/user_list.html', {'filter': user_filter})
3. Urls.py
from django.conf.urls import url
from mysite.search import views
urlpatterns = [
url(r'^search/$', views.search, name='search'),
]
4. User_list.html
{% block content %}
-
{% for user in filter.qs %}
- {{ user.username }} - {{ user.get_full_name }}
{% endfor %}
{% endblock %}
Or this