├── mysite ├── __init__.py ├── search │ ├── __init__.py │ ├── migrations │ │ └── __init__.py │ ├── views.py │ ├── admin.py │ ├── tests.py │ ├── models.py │ ├── apps.py │ ├── filters.py │ └── templates │ │ └── search │ │ └── user_list.html ├── static │ ├── fonts │ │ ├── glyphicons-halflings-regular.eot │ │ ├── glyphicons-halflings-regular.ttf │ │ ├── glyphicons-halflings-regular.woff │ │ └── glyphicons-halflings-regular.woff2 │ └── js │ │ ├── bootstrap.min.js │ │ └── jquery-3.1.1.min.js ├── templates │ ├── home.html │ ├── includes │ │ └── header.html │ └── base.html ├── wsgi.py ├── urls.py └── settings.py ├── requirements.txt ├── README.md ├── manage.py ├── .gitignore └── LICENSE /mysite/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /mysite/search/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /mysite/search/migrations/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /mysite/search/views.py: -------------------------------------------------------------------------------- 1 | from django.shortcuts import render 2 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | Django==1.10.3 2 | django-filter==1.0.0 3 | django-widget-tweaks==1.4.1 4 | -------------------------------------------------------------------------------- /mysite/search/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | 3 | # Register your models here. 4 | -------------------------------------------------------------------------------- /mysite/search/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /mysite/search/models.py: -------------------------------------------------------------------------------- 1 | from __future__ import unicode_literals 2 | 3 | from django.db import models 4 | 5 | # Create your models here. 6 | -------------------------------------------------------------------------------- /mysite/static/fonts/glyphicons-halflings-regular.eot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sibtc/simple-django-filter/HEAD/mysite/static/fonts/glyphicons-halflings-regular.eot -------------------------------------------------------------------------------- /mysite/static/fonts/glyphicons-halflings-regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sibtc/simple-django-filter/HEAD/mysite/static/fonts/glyphicons-halflings-regular.ttf -------------------------------------------------------------------------------- /mysite/static/fonts/glyphicons-halflings-regular.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sibtc/simple-django-filter/HEAD/mysite/static/fonts/glyphicons-halflings-regular.woff -------------------------------------------------------------------------------- /mysite/static/fonts/glyphicons-halflings-regular.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sibtc/simple-django-filter/HEAD/mysite/static/fonts/glyphicons-halflings-regular.woff2 -------------------------------------------------------------------------------- /mysite/search/apps.py: -------------------------------------------------------------------------------- 1 | from __future__ import unicode_literals 2 | 3 | from django.apps import AppConfig 4 | 5 | 6 | class SearchConfig(AppConfig): 7 | name = 'search' 8 | -------------------------------------------------------------------------------- /mysite/templates/home.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | 3 | {% block content %} 4 |
Click on the Search menu to explore the example.
6 | {% endblock %} -------------------------------------------------------------------------------- /mysite/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for mysite project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings") 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Django Filter Example 2 | 3 | Example used in the blog post [How to Filter QuerySets Dynamically](https://simpleisbetterthancomplex.com/tutorial/2016/11/28/how-to-filter-querysets-dynamically.html) 4 | 5 | ## Running Locally 6 | 7 | ```bash 8 | git clone https://github.com/sibtc/repository-name.git 9 | ``` 10 | 11 | ```bash 12 | pip install -r requirements.txt 13 | ``` 14 | 15 | ```bash 16 | python manage.py migrate 17 | ``` 18 | 19 | ```bash 20 | python manage.py runserver 21 | ``` 22 | -------------------------------------------------------------------------------- /mysite/urls.py: -------------------------------------------------------------------------------- 1 | from django.conf.urls import url, include 2 | from django.contrib import admin 3 | from django.views.generic import TemplateView 4 | 5 | from django_filters.views import FilterView 6 | 7 | from mysite.search.filters import UserFilter 8 | 9 | 10 | urlpatterns = [ 11 | url(r'^$', TemplateView.as_view(template_name='home.html'), name='home'), 12 | url(r'^search/$', FilterView.as_view(filterset_class=UserFilter, template_name='search/user_list.html'), name='search'), 13 | url(r'^admin/', include(admin.site.urls)), 14 | ] 15 | -------------------------------------------------------------------------------- /mysite/search/filters.py: -------------------------------------------------------------------------------- 1 | from django import forms 2 | from django.contrib.auth.models import User, Group 3 | 4 | import django_filters 5 | 6 | 7 | class UserFilter(django_filters.FilterSet): 8 | first_name = django_filters.CharFilter(lookup_expr='icontains') 9 | year_joined = django_filters.NumberFilter(name='date_joined', lookup_expr='year') 10 | groups = django_filters.ModelMultipleChoiceFilter(queryset=Group.objects.all(), widget=forms.CheckboxSelectMultiple) 11 | 12 | class Meta: 13 | model = User 14 | fields = ['username', 'first_name', 'last_name', 'year_joined', 'groups'] 15 | -------------------------------------------------------------------------------- /manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import os 3 | import sys 4 | 5 | if __name__ == "__main__": 6 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings") 7 | try: 8 | from django.core.management import execute_from_command_line 9 | except ImportError: 10 | # The above import may fail for some other reason. Ensure that the 11 | # issue is really that Django is missing to avoid masking other 12 | # exceptions on Python 2. 13 | try: 14 | import django 15 | except ImportError: 16 | raise ImportError( 17 | "Couldn't import Django. Are you sure it's installed and " 18 | "available on your PYTHONPATH environment variable? Did you " 19 | "forget to activate a virtual environment?" 20 | ) 21 | raise 22 | execute_from_command_line(sys.argv) 23 | -------------------------------------------------------------------------------- /mysite/templates/includes/header.html: -------------------------------------------------------------------------------- 1 | 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | 27 | # PyInstaller 28 | # Usually these files are written by a python script from a template 29 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 30 | *.manifest 31 | *.spec 32 | 33 | # Installer logs 34 | pip-log.txt 35 | pip-delete-this-directory.txt 36 | 37 | # Unit test / coverage reports 38 | htmlcov/ 39 | .tox/ 40 | .coverage 41 | .coverage.* 42 | .cache 43 | nosetests.xml 44 | coverage.xml 45 | *,cover 46 | .hypothesis/ 47 | 48 | # Translations 49 | *.mo 50 | *.pot 51 | 52 | # Django stuff: 53 | *.log 54 | 55 | # Sphinx documentation 56 | docs/_build/ 57 | 58 | # PyBuilder 59 | target/ 60 | 61 | #Ipython Notebook 62 | .ipynb_checkpoints 63 | 64 | .DS_Store 65 | .env 66 | *.sqlite3 -------------------------------------------------------------------------------- /mysite/templates/base.html: -------------------------------------------------------------------------------- 1 | {% load static %} 2 | 3 | 4 | 5 | 6 | 7 || Username | 47 |First name | 48 |Last name | 49 |Date joined | 50 |Groups | 51 |
|---|---|---|---|---|
| {{ user.username }} | 57 |{{ user.first_name }} | 58 |{{ user.last_name }} | 59 |{{ user.date_joined }} | 60 |61 | {% for group in user.groups.all %} 62 | {{ group }} 63 | {% empty %} 64 | No group 65 | {% endfor %} 66 | | 67 |
| No data | 71 |||||