├── .gitignore ├── cities ├── __init__.py ├── migrations │ ├── __init__.py │ └── 0001_initial.py ├── tests.py ├── apps.py ├── admin.py ├── urls.py ├── models.py └── views.py ├── citysearch_project ├── __init__.py ├── urls.py ├── wsgi.py └── settings.py ├── db.sqlite3 ├── README.md ├── templates ├── home.html └── search_results.html ├── Pipfile ├── manage.py └── Pipfile.lock /.gitignore: -------------------------------------------------------------------------------- 1 | vscode -------------------------------------------------------------------------------- /cities/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /cities/migrations/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /citysearch_project/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /cities/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /db.sqlite3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wsvincent/django-search-tutorial/HEAD/db.sqlite3 -------------------------------------------------------------------------------- /cities/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class CitiesConfig(AppConfig): 5 | name = 'cities' 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Django Search Tutorial 2 | 3 | Source code for tutorial found on [LearnDjango.com](https://learndjango.com/tutorials/django-search-tutorial). 4 | -------------------------------------------------------------------------------- /templates/home.html: -------------------------------------------------------------------------------- 1 |

HomePage

2 | 3 |
4 | 5 |
-------------------------------------------------------------------------------- /templates/search_results.html: -------------------------------------------------------------------------------- 1 |

Search Results

2 | 3 | -------------------------------------------------------------------------------- /cities/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | 3 | from .models import City 4 | 5 | class CityAdmin(admin.ModelAdmin): 6 | list_display = ("name", "state",) 7 | 8 | admin.site.register(City, CityAdmin) -------------------------------------------------------------------------------- /citysearch_project/urls.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | from django.urls import path, include 3 | 4 | urlpatterns = [ 5 | path('admin/', admin.site.urls), 6 | path('', include('cities.urls')), 7 | ] -------------------------------------------------------------------------------- /Pipfile: -------------------------------------------------------------------------------- 1 | [[source]] 2 | name = "pypi" 3 | url = "https://pypi.org/simple" 4 | verify_ssl = true 5 | 6 | [dev-packages] 7 | 8 | [packages] 9 | django = "~=3.1.0" 10 | 11 | [requires] 12 | python_version = "3.7" 13 | -------------------------------------------------------------------------------- /cities/urls.py: -------------------------------------------------------------------------------- 1 | from django.urls import path 2 | 3 | from .views import HomePageView, SearchResultsView 4 | 5 | urlpatterns = [ 6 | path('search/', SearchResultsView.as_view(), name='search_results'), 7 | path('', HomePageView.as_view(), name='home'), 8 | ] -------------------------------------------------------------------------------- /cities/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | 4 | class City(models.Model): 5 | name = models.CharField(max_length=255) 6 | state = models.CharField(max_length=255) 7 | 8 | class Meta: 9 | verbose_name_plural = "cities" 10 | 11 | def __str__(self): 12 | return self.name -------------------------------------------------------------------------------- /citysearch_project/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for citysearch_project 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/2.2/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', 'citysearch_project.settings') 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /cities/views.py: -------------------------------------------------------------------------------- 1 | from django.db.models import Q 2 | from django.views.generic import TemplateView, ListView 3 | 4 | from .models import City 5 | 6 | 7 | class HomePageView(TemplateView): 8 | template_name = 'home.html' 9 | 10 | class SearchResultsView(ListView): 11 | model = City 12 | template_name = 'search_results.html' 13 | 14 | def get_queryset(self): 15 | query = self.request.GET.get('q') 16 | object_list = City.objects.filter( 17 | Q(name__icontains=query) | Q(state__icontains=query) 18 | ) 19 | return object_list -------------------------------------------------------------------------------- /manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Django's command-line utility for administrative tasks.""" 3 | import os 4 | import sys 5 | 6 | 7 | def main(): 8 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'citysearch_project.settings') 9 | try: 10 | from django.core.management import execute_from_command_line 11 | except ImportError as exc: 12 | raise ImportError( 13 | "Couldn't import Django. Are you sure it's installed and " 14 | "available on your PYTHONPATH environment variable? Did you " 15 | "forget to activate a virtual environment?" 16 | ) from exc 17 | execute_from_command_line(sys.argv) 18 | 19 | 20 | if __name__ == '__main__': 21 | main() 22 | -------------------------------------------------------------------------------- /cities/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 2.2.1 on 2019-05-10 13:56 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | initial = True 9 | 10 | dependencies = [ 11 | ] 12 | 13 | operations = [ 14 | migrations.CreateModel( 15 | name='City', 16 | fields=[ 17 | ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 18 | ('name', models.CharField(max_length=255)), 19 | ('state', models.CharField(max_length=255)), 20 | ], 21 | options={ 22 | 'verbose_name_plural': 'cities', 23 | }, 24 | ), 25 | ] 26 | -------------------------------------------------------------------------------- /Pipfile.lock: -------------------------------------------------------------------------------- 1 | { 2 | "_meta": { 3 | "hash": { 4 | "sha256": "b82fc26acd9efb9bd0fc52265bc322306e184d9033c38ce26ff955d61e0d1669" 5 | }, 6 | "pipfile-spec": 6, 7 | "requires": { 8 | "python_version": "3.7" 9 | }, 10 | "sources": [ 11 | { 12 | "name": "pypi", 13 | "url": "https://pypi.org/simple", 14 | "verify_ssl": true 15 | } 16 | ] 17 | }, 18 | "default": { 19 | "asgiref": { 20 | "hashes": [ 21 | "sha256:7e51911ee147dd685c3c8b805c0ad0cb58d360987b56953878f8c06d2d1c6f1a", 22 | "sha256:9fc6fb5d39b8af147ba40765234fa822b39818b12cc80b35ad9b0cef3a476aed" 23 | ], 24 | "markers": "python_version >= '3.5'", 25 | "version": "==3.2.10" 26 | }, 27 | "django": { 28 | "hashes": [ 29 | "sha256:59c8125ca873ed3bdae9c12b146fbbd6ed8d0f743e4cf5f5817af50c51f1fc2f", 30 | "sha256:b5fbb818e751f660fa2d576d9f40c34a4c615c8b48dd383f5216e609f383371f" 31 | ], 32 | "index": "pypi", 33 | "version": "==3.1.1" 34 | }, 35 | "pytz": { 36 | "hashes": [ 37 | "sha256:a494d53b6d39c3c6e44c3bec237336e14305e4f29bbf800b599253057fbb79ed", 38 | "sha256:c35965d010ce31b23eeb663ed3cc8c906275d6be1a34393a1d73a41febf4a048" 39 | ], 40 | "version": "==2020.1" 41 | }, 42 | "sqlparse": { 43 | "hashes": [ 44 | "sha256:022fb9c87b524d1f7862b3037e541f68597a730a8843245c349fc93e1643dc4e", 45 | "sha256:e162203737712307dfe78860cc56c8da8a852ab2ee33750e33aeadf38d12c548" 46 | ], 47 | "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'", 48 | "version": "==0.3.1" 49 | } 50 | }, 51 | "develop": {} 52 | } 53 | -------------------------------------------------------------------------------- /citysearch_project/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for citysearch_project project. 3 | 4 | Generated by 'django-admin startproject' using Django 2.2.1. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/2.2/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/2.2/ref/settings/ 11 | """ 12 | 13 | import os 14 | 15 | # Build paths inside the project like this: os.path.join(BASE_DIR, ...) 16 | BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 17 | 18 | 19 | # Quick-start development settings - unsuitable for production 20 | # See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/ 21 | 22 | # SECURITY WARNING: keep the secret key used in production secret! 23 | SECRET_KEY = '&21rh90=)%ridsb9m#v32ft#i@sldw-(dzdu90a#2=yquks8$@' 24 | 25 | # SECURITY WARNING: don't run with debug turned on in production! 26 | DEBUG = True 27 | 28 | ALLOWED_HOSTS = [] 29 | 30 | 31 | # Application definition 32 | 33 | INSTALLED_APPS = [ 34 | 'django.contrib.admin', 35 | 'django.contrib.auth', 36 | 'django.contrib.contenttypes', 37 | 'django.contrib.sessions', 38 | 'django.contrib.messages', 39 | 'django.contrib.staticfiles', 40 | 'cities.apps.CitiesConfig', 41 | ] 42 | 43 | MIDDLEWARE = [ 44 | 'django.middleware.security.SecurityMiddleware', 45 | 'django.contrib.sessions.middleware.SessionMiddleware', 46 | 'django.middleware.common.CommonMiddleware', 47 | 'django.middleware.csrf.CsrfViewMiddleware', 48 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 49 | 'django.contrib.messages.middleware.MessageMiddleware', 50 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 51 | ] 52 | 53 | ROOT_URLCONF = 'citysearch_project.urls' 54 | 55 | TEMPLATES = [ 56 | { 57 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 58 | 'DIRS': [os.path.join(BASE_DIR, 'templates')], 59 | 'APP_DIRS': True, 60 | 'OPTIONS': { 61 | 'context_processors': [ 62 | 'django.template.context_processors.debug', 63 | 'django.template.context_processors.request', 64 | 'django.contrib.auth.context_processors.auth', 65 | 'django.contrib.messages.context_processors.messages', 66 | ], 67 | }, 68 | }, 69 | ] 70 | 71 | WSGI_APPLICATION = 'citysearch_project.wsgi.application' 72 | 73 | 74 | # Database 75 | # https://docs.djangoproject.com/en/2.2/ref/settings/#databases 76 | 77 | DATABASES = { 78 | 'default': { 79 | 'ENGINE': 'django.db.backends.sqlite3', 80 | 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 81 | } 82 | } 83 | 84 | 85 | # Password validation 86 | # https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators 87 | 88 | AUTH_PASSWORD_VALIDATORS = [ 89 | { 90 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 91 | }, 92 | { 93 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 94 | }, 95 | { 96 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 97 | }, 98 | { 99 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 100 | }, 101 | ] 102 | 103 | 104 | # Internationalization 105 | # https://docs.djangoproject.com/en/2.2/topics/i18n/ 106 | 107 | LANGUAGE_CODE = 'en-us' 108 | 109 | TIME_ZONE = 'UTC' 110 | 111 | USE_I18N = True 112 | 113 | USE_L10N = True 114 | 115 | USE_TZ = True 116 | 117 | 118 | # Static files (CSS, JavaScript, Images) 119 | # https://docs.djangoproject.com/en/2.2/howto/static-files/ 120 | 121 | STATIC_URL = '/static/' 122 | --------------------------------------------------------------------------------