├── polls ├── __init__.py ├── migrations │ ├── __init__.py │ └── 0001_initial.py ├── tests.py ├── apps.py ├── urls.py ├── views.py ├── templates │ └── polls │ │ └── poll_list.html ├── api │ ├── serializers.py │ ├── views.py │ └── urls.py ├── admin.py └── models.py ├── subdomains_tutorial ├── __init__.py ├── api_urls.py ├── frontend_urls.py ├── hosts.py ├── wsgi.py ├── urls.py └── settings.py ├── .gitignore └── manage.py /polls/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /polls/migrations/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /subdomains_tutorial/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /polls/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /polls/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class PollsConfig(AppConfig): 5 | name = 'polls' 6 | -------------------------------------------------------------------------------- /subdomains_tutorial/api_urls.py: -------------------------------------------------------------------------------- 1 | from django.urls import path, include 2 | 3 | urlpatterns = [ 4 | path('', include('polls.api.urls')), 5 | ] 6 | -------------------------------------------------------------------------------- /subdomains_tutorial/frontend_urls.py: -------------------------------------------------------------------------------- 1 | from django.urls import path, include 2 | 3 | urlpatterns = [ 4 | path('', include('polls.urls')), 5 | ] 6 | -------------------------------------------------------------------------------- /polls/urls.py: -------------------------------------------------------------------------------- 1 | from django.urls import path 2 | 3 | from polls import views 4 | 5 | urlpatterns = [ 6 | path('polls', views.PollListView.as_view()) 7 | ] 8 | -------------------------------------------------------------------------------- /polls/views.py: -------------------------------------------------------------------------------- 1 | from django.views import generic 2 | 3 | from polls.models import Poll 4 | 5 | 6 | class PollListView(generic.ListView): 7 | model = Poll 8 | -------------------------------------------------------------------------------- /polls/templates/polls/poll_list.html: -------------------------------------------------------------------------------- 1 | {% if object_list %} 2 | {% for object in object_list %} 3 |
  • {{ object }}
  • 4 | {% endfor %} 5 | {% endif %} -------------------------------------------------------------------------------- /polls/api/serializers.py: -------------------------------------------------------------------------------- 1 | from rest_framework import serializers 2 | 3 | from polls.models import Poll 4 | 5 | 6 | class PollSerializer(serializers.ModelSerializer): 7 | class Meta: 8 | model = Poll 9 | fields = '__all__' 10 | -------------------------------------------------------------------------------- /polls/api/views.py: -------------------------------------------------------------------------------- 1 | from rest_framework import viewsets 2 | 3 | from polls.api.serializers import PollSerializer 4 | from polls.models import Poll 5 | 6 | 7 | class PollViewSet(viewsets.ModelViewSet): 8 | serializer_class = PollSerializer 9 | queryset = Poll.objects 10 | -------------------------------------------------------------------------------- /polls/api/urls.py: -------------------------------------------------------------------------------- 1 | from django.urls import include, path 2 | from rest_framework import routers 3 | 4 | from polls.api import views 5 | 6 | router = routers.SimpleRouter() 7 | router.register(r'polls', views.PollViewSet, base_name='polls') 8 | 9 | urlpatterns = [ 10 | path('', include(router.urls)), 11 | ] 12 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea/ 2 | 3 | # Byte-compiled / optimized / DLL files 4 | __pycache__/ 5 | *.py[cod] 6 | *$py.class 7 | 8 | # Installer logs 9 | pip-log.txt 10 | pip-delete-this-directory.txt 11 | 12 | 13 | # Django stuff: 14 | *.log 15 | local_settings.py 16 | db.sqlite3 17 | 18 | .env 19 | /venv 20 | /uploads/ 21 | /staticfiles/ 22 | -------------------------------------------------------------------------------- /polls/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | 3 | from polls.models import PollOption, Poll 4 | 5 | 6 | class PollOptionInline(admin.TabularInline): 7 | model = PollOption 8 | fields = ('value',) 9 | 10 | 11 | class PollAdmin(admin.ModelAdmin): 12 | inlines = (PollOptionInline,) 13 | 14 | 15 | admin.site.register(Poll, PollAdmin) 16 | -------------------------------------------------------------------------------- /subdomains_tutorial/hosts.py: -------------------------------------------------------------------------------- 1 | from django.conf import settings 2 | from django_hosts import patterns, host 3 | 4 | host_patterns = patterns( 5 | '', 6 | host(r'www', 'subdomains_tutorial.frontend_urls', name='www'), 7 | host(r'admin', settings.ROOT_URLCONF, name='admin'), 8 | host(r'api', 'subdomains_tutorial.api_urls', name='api'), 9 | ) 10 | -------------------------------------------------------------------------------- /polls/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | 4 | class Poll(models.Model): 5 | content = models.CharField(max_length=128) 6 | 7 | def __str__(self): 8 | return self.content 9 | 10 | 11 | class PollOption(models.Model): 12 | poll = models.ForeignKey(Poll, on_delete=models.CASCADE) 13 | value = models.CharField(max_length=128) 14 | 15 | def __str__(self): 16 | return self.value 17 | -------------------------------------------------------------------------------- /subdomains_tutorial/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for subdomains_tutorial 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.1/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', 'subdomains_tutorial.settings') 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /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', 'subdomains_tutorial.settings') 7 | try: 8 | from django.core.management import execute_from_command_line 9 | except ImportError as exc: 10 | raise ImportError( 11 | "Couldn't import Django. Are you sure it's installed and " 12 | "available on your PYTHONPATH environment variable? Did you " 13 | "forget to activate a virtual environment?" 14 | ) from exc 15 | execute_from_command_line(sys.argv) 16 | -------------------------------------------------------------------------------- /subdomains_tutorial/urls.py: -------------------------------------------------------------------------------- 1 | """subdomains_tutorial URL Configuration 2 | 3 | The `urlpatterns` list routes URLs to views. For more information please see: 4 | https://docs.djangoproject.com/en/2.1/topics/http/urls/ 5 | Examples: 6 | Function views 7 | 1. Add an import: from my_app import views 8 | 2. Add a URL to urlpatterns: path('', views.home, name='home') 9 | Class-based views 10 | 1. Add an import: from other_app.views import Home 11 | 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') 12 | Including another URLconf 13 | 1. Import the include() function: from django.urls import include, path 14 | 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 15 | """ 16 | from django.contrib import admin 17 | from django.urls import path 18 | 19 | urlpatterns = [ 20 | path('', admin.site.urls), 21 | ] 22 | -------------------------------------------------------------------------------- /polls/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 2.1.2 on 2018-10-01 19:34 2 | 3 | from django.db import migrations, models 4 | import django.db.models.deletion 5 | 6 | 7 | class Migration(migrations.Migration): 8 | 9 | initial = True 10 | 11 | dependencies = [ 12 | ] 13 | 14 | operations = [ 15 | migrations.CreateModel( 16 | name='Poll', 17 | fields=[ 18 | ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 19 | ('content', models.CharField(max_length=128)), 20 | ], 21 | ), 22 | migrations.CreateModel( 23 | name='PollOption', 24 | fields=[ 25 | ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 26 | ('value', models.CharField(max_length=128)), 27 | ('poll', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='polls.Poll')), 28 | ], 29 | ), 30 | ] 31 | -------------------------------------------------------------------------------- /subdomains_tutorial/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for subdomains_tutorial project. 3 | 4 | Generated by 'django-admin startproject' using Django 2.1.2. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/2.1/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/2.1/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 | # Quick-start development settings - unsuitable for production 19 | # See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/ 20 | 21 | # SECURITY WARNING: keep the secret key used in production secret! 22 | SECRET_KEY = '*3(ut2bjfwj8^dkip@j)yas8$818m&+3=$(d4v*@we(=095gj9' 23 | 24 | # SECURITY WARNING: don't run with debug turned on in production! 25 | DEBUG = True 26 | 27 | ALLOWED_HOSTS = ['*'] 28 | 29 | # Application definition 30 | 31 | INSTALLED_APPS = [ 32 | 'django.contrib.admin', 33 | 'django.contrib.auth', 34 | 'django.contrib.contenttypes', 35 | 'django.contrib.sessions', 36 | 'django.contrib.messages', 37 | 'django.contrib.staticfiles', 38 | 39 | 'rest_framework', 40 | 'django_hosts', 41 | 42 | 'polls', 43 | ] 44 | 45 | MIDDLEWARE = [ 46 | 'django_hosts.middleware.HostsRequestMiddleware', 47 | 'django.middleware.security.SecurityMiddleware', 48 | 'django.contrib.sessions.middleware.SessionMiddleware', 49 | 'django.middleware.common.CommonMiddleware', 50 | 'django.middleware.csrf.CsrfViewMiddleware', 51 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 52 | 'django.contrib.messages.middleware.MessageMiddleware', 53 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 54 | 'django_hosts.middleware.HostsResponseMiddleware', 55 | ] 56 | 57 | ROOT_URLCONF = 'subdomains_tutorial.urls' 58 | ROOT_HOSTCONF = 'subdomains_tutorial.hosts' 59 | 60 | DEFAULT_HOST = 'www' 61 | 62 | TEMPLATES = [ 63 | { 64 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 65 | 'DIRS': [], 66 | 'APP_DIRS': True, 67 | 'OPTIONS': { 68 | 'context_processors': [ 69 | 'django.template.context_processors.debug', 70 | 'django.template.context_processors.request', 71 | 'django.contrib.auth.context_processors.auth', 72 | 'django.contrib.messages.context_processors.messages', 73 | ], 74 | }, 75 | }, 76 | ] 77 | 78 | WSGI_APPLICATION = 'subdomains_tutorial.wsgi.application' 79 | 80 | # Database 81 | # https://docs.djangoproject.com/en/2.1/ref/settings/#databases 82 | 83 | DATABASES = { 84 | 'default': { 85 | 'ENGINE': 'django.db.backends.sqlite3', 86 | 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 87 | } 88 | } 89 | 90 | # Password validation 91 | # https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators 92 | 93 | AUTH_PASSWORD_VALIDATORS = [ 94 | { 95 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 96 | }, 97 | { 98 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 99 | }, 100 | { 101 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 102 | }, 103 | { 104 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 105 | }, 106 | ] 107 | 108 | # Internationalization 109 | # https://docs.djangoproject.com/en/2.1/topics/i18n/ 110 | 111 | LANGUAGE_CODE = 'en-us' 112 | 113 | TIME_ZONE = 'UTC' 114 | 115 | USE_I18N = True 116 | 117 | USE_L10N = True 118 | 119 | USE_TZ = True 120 | 121 | # Static files (CSS, JavaScript, Images) 122 | # https://docs.djangoproject.com/en/2.1/howto/static-files/ 123 | 124 | STATIC_URL = '/static/' 125 | --------------------------------------------------------------------------------