├── mysite ├── mysite │ ├── __init__.py │ ├── wsgi.py │ ├── urls.py │ └── settings.py ├── polls │ ├── __init__.py │ ├── migrations │ │ ├── __init__.py │ │ └── 0001_initial.py │ ├── apps.py │ ├── static │ │ └── polls │ │ │ ├── images │ │ │ └── background.gif │ │ │ └── style.css │ ├── templates │ │ └── polls │ │ │ ├── results.html │ │ │ ├── index.html │ │ │ └── detail.html │ ├── urls.py │ ├── admin.py │ ├── models.py │ ├── views.py │ └── tests.py ├── templates │ └── admin │ │ └── base_site.html └── manage.py ├── LICENSE ├── .gitignore └── README.md /mysite/mysite/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /mysite/polls/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /mysite/polls/migrations/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /mysite/polls/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class PollsConfig(AppConfig): 5 | name = 'polls' 6 | -------------------------------------------------------------------------------- /mysite/polls/static/polls/images/background.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/consideratecode/django-tutorial-step-by-step/HEAD/mysite/polls/static/polls/images/background.gif -------------------------------------------------------------------------------- /mysite/polls/static/polls/style.css: -------------------------------------------------------------------------------- 1 | li a { 2 | color: green; 3 | } 4 | 5 | body { 6 | background: white url("images/background.gif") no-repeat right bottom; 7 | } 8 | -------------------------------------------------------------------------------- /mysite/polls/templates/polls/results.html: -------------------------------------------------------------------------------- 1 |

{{ question.question_text }}

2 | 3 | 8 | 9 | Vote again? 10 | -------------------------------------------------------------------------------- /mysite/templates/admin/base_site.html: -------------------------------------------------------------------------------- 1 | {% extends "admin/base.html" %} 2 | 3 | {% block title %}{{ title }} | {{ site_title|default:_('Django site admin') }}{% endblock %} 4 | 5 | {% block branding %} 6 |

Polls Administration

7 | {% endblock %} 8 | 9 | {% block nav-global %}{% endblock %} 10 | -------------------------------------------------------------------------------- /mysite/polls/urls.py: -------------------------------------------------------------------------------- 1 | from django.urls import path 2 | 3 | from . import views 4 | 5 | app_name = 'polls' 6 | urlpatterns = [ 7 | path('', views.IndexView.as_view(), name='index'), 8 | path('/', views.DetailView.as_view(), name='detail'), 9 | path('/results/', views.ResultsView.as_view(), name='results'), 10 | path('/vote/', views.vote, name='vote'), 11 | ] 12 | -------------------------------------------------------------------------------- /mysite/polls/templates/polls/index.html: -------------------------------------------------------------------------------- 1 | {% load static %} 2 | 3 | 4 | 5 | {% if latest_question_list %} 6 | 11 | {% else %} 12 |

No polls are available.

13 | {% endif %} 14 | -------------------------------------------------------------------------------- /mysite/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/2.0/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 | -------------------------------------------------------------------------------- /mysite/polls/templates/polls/detail.html: -------------------------------------------------------------------------------- 1 |

{{ question.question_text }}

2 | 3 | {% if error_message %}

{{ error_message }}

{% endif %} 4 | 5 |
6 | {% csrf_token %} 7 | {% for choice in question.choice_set.all %} 8 | 9 |
10 | {% endfor %} 11 | 12 |
13 | -------------------------------------------------------------------------------- /mysite/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 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 | -------------------------------------------------------------------------------- /mysite/polls/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | 3 | from .models import Choice, Question 4 | 5 | 6 | class ChoiceInline(admin.TabularInline): 7 | model = Choice 8 | extra = 3 9 | 10 | 11 | class QuestionAdmin(admin.ModelAdmin): 12 | fieldsets = [ 13 | (None, {'fields': ['question_text']}), 14 | ('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}), 15 | ] 16 | inlines = [ChoiceInline] 17 | list_display = ('question_text', 'pub_date', 'was_published_recently') 18 | list_filter = ['pub_date'] 19 | search_fields = ['question_text'] 20 | 21 | admin.site.register(Question, QuestionAdmin) 22 | -------------------------------------------------------------------------------- /mysite/mysite/urls.py: -------------------------------------------------------------------------------- 1 | """mysite URL Configuration 2 | 3 | The `urlpatterns` list routes URLs to views. For more information please see: 4 | https://docs.djangoproject.com/en/2.0/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.urls import include, path 17 | from django.contrib import admin 18 | 19 | urlpatterns = [ 20 | path('polls/', include('polls.urls')), 21 | path('admin/', admin.site.urls), 22 | ] 23 | -------------------------------------------------------------------------------- /mysite/polls/models.py: -------------------------------------------------------------------------------- 1 | import datetime 2 | 3 | from django.db import models 4 | from django.utils import timezone 5 | 6 | 7 | class Question(models.Model): 8 | question_text = models.CharField(max_length=200) 9 | pub_date = models.DateTimeField('date published') 10 | 11 | def __str__(self): 12 | return self.question_text 13 | 14 | def was_published_recently(self): 15 | now = timezone.now() 16 | return now - datetime.timedelta(days=1) <= self.pub_date <= now 17 | was_published_recently.admin_order_field = 'pub_date' 18 | was_published_recently.boolean = True 19 | was_published_recently.short_description = 'Published recently?' 20 | 21 | 22 | class Choice(models.Model): 23 | question = models.ForeignKey(Question, on_delete=models.CASCADE) 24 | choice_text = models.CharField(max_length=200) 25 | votes = models.IntegerField(default=0) 26 | 27 | def __str__(self): 28 | return self.choice_text 29 | -------------------------------------------------------------------------------- /mysite/polls/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 2.0 on 2017-12-11 12:01 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='Choice', 17 | fields=[ 18 | ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 19 | ('choice_text', models.CharField(max_length=200)), 20 | ('votes', models.IntegerField(default=0)), 21 | ], 22 | ), 23 | migrations.CreateModel( 24 | name='Question', 25 | fields=[ 26 | ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 27 | ('question_text', models.CharField(max_length=200)), 28 | ('pub_date', models.DateTimeField(verbose_name='date published')), 29 | ], 30 | ), 31 | migrations.AddField( 32 | model_name='choice', 33 | name='question', 34 | field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='polls.Question'), 35 | ), 36 | ] 37 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) Django Software Foundation and individual contributors. 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, 5 | are permitted provided that the following conditions are met: 6 | 7 | 1. Redistributions of source code must retain the above copyright notice, 8 | this list of conditions and the following disclaimer. 9 | 10 | 2. Redistributions in binary form must reproduce the above copyright 11 | notice, this list of conditions and the following disclaimer in the 12 | documentation and/or other materials provided with the distribution. 13 | 14 | 3. Neither the name of Django nor the names of its contributors may be used 15 | to endorse or promote products derived from this software without 16 | specific prior written permission. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 19 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 20 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 22 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 25 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 27 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | db.sqlite3 2 | # Byte-compiled / optimized / DLL files 3 | __pycache__/ 4 | *.py[cod] 5 | *$py.class 6 | 7 | # C extensions 8 | *.so 9 | 10 | # Distribution / packaging 11 | .Python 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .coverage 43 | .coverage.* 44 | .cache 45 | nosetests.xml 46 | coverage.xml 47 | *.cover 48 | .hypothesis/ 49 | 50 | # Translations 51 | *.mo 52 | *.pot 53 | 54 | # Django stuff: 55 | *.log 56 | .static_storage/ 57 | .media/ 58 | local_settings.py 59 | 60 | # Flask stuff: 61 | instance/ 62 | .webassets-cache 63 | 64 | # Scrapy stuff: 65 | .scrapy 66 | 67 | # Sphinx documentation 68 | docs/_build/ 69 | 70 | # PyBuilder 71 | target/ 72 | 73 | # Jupyter Notebook 74 | .ipynb_checkpoints 75 | 76 | # pyenv 77 | .python-version 78 | 79 | # celery beat schedule file 80 | celerybeat-schedule 81 | 82 | # SageMath parsed files 83 | *.sage.py 84 | 85 | # Environments 86 | .env 87 | .venv 88 | env/ 89 | venv/ 90 | ENV/ 91 | env.bak/ 92 | venv.bak/ 93 | 94 | # Spyder project settings 95 | .spyderproject 96 | .spyproject 97 | 98 | # Rope project settings 99 | .ropeproject 100 | 101 | # mkdocs documentation 102 | /site 103 | 104 | # mypy 105 | .mypy_cache/ 106 | -------------------------------------------------------------------------------- /mysite/polls/views.py: -------------------------------------------------------------------------------- 1 | from django.shortcuts import get_object_or_404, render 2 | from django.http import HttpResponseRedirect 3 | from django.urls import reverse 4 | from django.views import generic 5 | from django.utils import timezone 6 | 7 | from .models import Choice, Question 8 | 9 | 10 | class IndexView(generic.ListView): 11 | template_name = 'polls/index.html' 12 | context_object_name = 'latest_question_list' 13 | 14 | def get_queryset(self): 15 | """ 16 | Return the last five published questions (not including those set to be 17 | published in the future). 18 | """ 19 | return Question.objects.filter( 20 | pub_date__lte=timezone.now() 21 | ).order_by('-pub_date')[:5] 22 | 23 | 24 | class DetailView(generic.DetailView): 25 | model = Question 26 | template_name = 'polls/detail.html' 27 | 28 | def get_queryset(self): 29 | """ 30 | Excludes any questions that aren't published yet. 31 | """ 32 | return Question.objects.filter(pub_date__lte=timezone.now()) 33 | 34 | 35 | class ResultsView(generic.DetailView): 36 | model = Question 37 | template_name = 'polls/results.html' 38 | 39 | 40 | def vote(request, question_id): 41 | question = get_object_or_404(Question, pk=question_id) 42 | try: 43 | selected_choice = question.choice_set.get(pk=request.POST['choice']) 44 | except (KeyError, Choice.DoesNotExist): 45 | # Redisplay the question voting form. 46 | return render(request, 'polls/detail.html', { 47 | 'question': question, 48 | 'error_message': "You didn't select a choice.", 49 | }) 50 | else: 51 | selected_choice.votes += 1 52 | selected_choice.save() 53 | # Always return an HttpResponseRedirect after successfully dealing 54 | # with POST data. This prevents data from being posted twice if a 55 | # user hits the Back button. 56 | return HttpResponseRedirect(reverse('polls:results', args=(question.id,))) 57 | -------------------------------------------------------------------------------- /mysite/mysite/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for mysite project. 3 | 4 | Generated by 'django-admin startproject' using Django 2.0. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/2.0/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/2.0/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.0/howto/deployment/checklist/ 21 | 22 | # SECURITY WARNING: keep the secret key used in production secret! 23 | SECRET_KEY = '-&=lf#8&wn1wu!ui1d!afb66p(nr%lu=&+ci_uroogeyz1ml)4' 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 | 'polls.apps.PollsConfig', 35 | 'django.contrib.admin', 36 | 'django.contrib.auth', 37 | 'django.contrib.contenttypes', 38 | 'django.contrib.sessions', 39 | 'django.contrib.messages', 40 | 'django.contrib.staticfiles', 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 = 'mysite.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 = 'mysite.wsgi.application' 72 | 73 | 74 | # Database 75 | # https://docs.djangoproject.com/en/2.0/ref/settings/#databases 76 | 77 | DATABASES = { 78 | 'default': { 79 | 'ENGINE': 'django.db.backends.sqlite3', # SQLite is fine for now 80 | 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 81 | } 82 | } 83 | 84 | 85 | # Password validation 86 | # https://docs.djangoproject.com/en/2.0/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.0/topics/i18n/ 106 | 107 | LANGUAGE_CODE = 'en-us' 108 | 109 | TIME_ZONE = 'Europe/Berlin' # Insert your timezone here 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.0/howto/static-files/ 120 | 121 | STATIC_URL = '/static/' 122 | -------------------------------------------------------------------------------- /mysite/polls/tests.py: -------------------------------------------------------------------------------- 1 | import datetime 2 | 3 | from django.utils import timezone 4 | from django.test import TestCase 5 | from django.urls import reverse 6 | 7 | from .models import Question 8 | 9 | 10 | class QuestionModelTests(TestCase): 11 | 12 | def test_was_published_recently_with_future_question(self): 13 | """ 14 | was_published_recently() returns False for questions whose pub_date 15 | is in the future. 16 | """ 17 | time = timezone.now() + datetime.timedelta(days=30) 18 | future_question = Question(pub_date=time) 19 | self.assertIs(future_question.was_published_recently(), False) 20 | 21 | def test_was_published_recently_with_old_question(self): 22 | """ 23 | was_published_recently() returns False for questions whose pub_date 24 | is older than 1 day. 25 | """ 26 | time = timezone.now() - datetime.timedelta(days=1, seconds=1) 27 | old_question = Question(pub_date=time) 28 | self.assertIs(old_question.was_published_recently(), False) 29 | 30 | def test_was_published_recently_with_recent_question(self): 31 | """ 32 | was_published_recently() returns True for questions whose pub_date 33 | is within the last day. 34 | """ 35 | time = timezone.now() - datetime.timedelta(hours=23, minutes=59, seconds=59) 36 | recent_question = Question(pub_date=time) 37 | self.assertIs(recent_question.was_published_recently(), True) 38 | 39 | 40 | def create_question(question_text, days): 41 | """ 42 | Create a question with the given `question_text` and published the 43 | given number of `days` offset to now (negative for questions published 44 | in the past, positive for questions that have yet to be published). 45 | """ 46 | time = timezone.now() + datetime.timedelta(days=days) 47 | return Question.objects.create(question_text=question_text, pub_date=time) 48 | 49 | 50 | class QuestionIndexViewTests(TestCase): 51 | def test_no_questions(self): 52 | """ 53 | If no questions exist, an appropriate message is displayed. 54 | """ 55 | response = self.client.get(reverse('polls:index')) 56 | self.assertEqual(response.status_code, 200) 57 | self.assertContains(response, "No polls are available.") 58 | self.assertQuerysetEqual(response.context['latest_question_list'], []) 59 | 60 | def test_past_question(self): 61 | """ 62 | Questions with a pub_date in the past are displayed on the 63 | index page. 64 | """ 65 | create_question(question_text="Past question.", days=-30) 66 | response = self.client.get(reverse('polls:index')) 67 | self.assertQuerysetEqual( 68 | response.context['latest_question_list'], 69 | [''] 70 | ) 71 | 72 | def test_future_question(self): 73 | """ 74 | Questions with a pub_date in the future aren't displayed on 75 | the index page. 76 | """ 77 | create_question(question_text="Future question.", days=30) 78 | response = self.client.get(reverse('polls:index')) 79 | self.assertContains(response, "No polls are available.") 80 | self.assertQuerysetEqual(response.context['latest_question_list'], []) 81 | 82 | def test_future_question_and_past_question(self): 83 | """ 84 | Even if both past and future questions exist, only past questions 85 | are displayed. 86 | """ 87 | create_question(question_text="Past question.", days=-30) 88 | create_question(question_text="Future question.", days=30) 89 | response = self.client.get(reverse('polls:index')) 90 | self.assertQuerysetEqual( 91 | response.context['latest_question_list'], 92 | [''] 93 | ) 94 | 95 | def test_two_past_questions(self): 96 | """ 97 | The questions index page may display multiple questions. 98 | """ 99 | create_question(question_text="Past question 1.", days=-30) 100 | create_question(question_text="Past question 2.", days=-5) 101 | response = self.client.get(reverse('polls:index')) 102 | self.assertQuerysetEqual( 103 | response.context['latest_question_list'], 104 | ['', ''] 105 | ) 106 | 107 | 108 | class QuestionDetailViewTests(TestCase): 109 | def test_future_question(self): 110 | """ 111 | The detail view of a question with a pub_date in the future 112 | returns a 404 not found. 113 | """ 114 | future_question = create_question(question_text='Future question.', days=5) 115 | url = reverse('polls:detail', args=(future_question.id,)) 116 | response = self.client.get(url) 117 | self.assertEqual(response.status_code, 404) 118 | 119 | def test_past_question(self): 120 | """ 121 | The detail view of a question with a pub_date in the past 122 | displays the question's text. 123 | """ 124 | past_question = create_question(question_text='Past Question.', days=-5) 125 | url = reverse('polls:detail', args=(past_question.id,)) 126 | response = self.client.get(url) 127 | self.assertContains(response, past_question.question_text) 128 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | A step-by-step solution for the official Django tutorial 2 | ======================================================== 3 | This repository contains the Django project you build in the official 4 | Django tutorial: the `mysite` project with the `polls` app. 5 | 6 | Each commit corresponds to a step in the tutorial. 7 | 8 | If you get stuck while following the tutorial, you can check what your 9 | code should look like - right at the step you are currently at. 10 | 11 | For further motivation, visit https://consideratecode.com/2017/12/15/django-tutorial-step-by-step 12 | 13 | How this repository is structured 14 | --------------------------------- 15 | There is a tagged commit for each complete code change. The commit message 16 | indicated which step of the tutorial the commit refers to. 17 | 18 | In most cases, each commit corresponds to the state of your project *after* a 19 | single section or sub-section in the tutorial. 20 | 21 | Sometimes, a section contains multiple code changes that you should try one 22 | after the other, e.g. the section ["Customize the admin change list"](https://docs.djangoproject.com/en/2.0/intro/tutorial07/#customize-the-admin-change-list) in Part 7 23 | corresponds to five commits. 24 | 25 | Table of contents: 26 | ------------------ 27 | Here is a list of all steps in the tutorial, linked to the corresponding tag. The name of each tag is shown in brackets. 28 | 29 | Part 1: 30 | * [Part 1: Creating a project](https://github.com/consideratecode/django-tutorial-step-by-step/tree/2.0/1.1) [2.0/1.1] 31 | * [Part 1: Creating the Polls app](https://github.com/consideratecode/django-tutorial-step-by-step/tree/2.0/1.2) [2.0/1.2] 32 | * [Part 1: Writing your first view](https://github.com/consideratecode/django-tutorial-step-by-step/tree/2.0/1.3) [2.0/1.3] 33 | 34 | Part 2: 35 | * [Part 2: Database setup](https://github.com/consideratecode/django-tutorial-step-by-step/tree/2.0/2.1) [2.0/2.1] 36 | * [Step 2: Creating models](https://github.com/consideratecode/django-tutorial-step-by-step/tree/2.0/2.2) [2.0/2.2] 37 | * [Part 2: Activating models](https://github.com/consideratecode/django-tutorial-step-by-step/tree/2.0/2.3) [2.0/2.3] 38 | * [Part 2: Playing with the API](https://github.com/consideratecode/django-tutorial-step-by-step/tree/2.0/2.4) [2.0/2.4] 39 | * [Part 2: Introducing the Django Admin](https://github.com/consideratecode/django-tutorial-step-by-step/tree/2.0/2.5) [2.0/2.5] 40 | 41 | Part 3: 42 | * [Part 3: Writing more views](https://github.com/consideratecode/django-tutorial-step-by-step/tree/2.0/3.1) [2.0/3.1] 43 | * [Part 3: Write views that actually do something](https://github.com/consideratecode/django-tutorial-step-by-step/tree/2.0/3.2) [2.0/3.2] 44 | * [Part 3: Write views that actually do something - with templates](https://github.com/consideratecode/django-tutorial-step-by-step/tree/2.0/3.2.1) [2.0/3.2.1] 45 | * [Part 3: Write views that actually do something - A shortcut: render()](https://github.com/consideratecode/django-tutorial-step-by-step/tree/2.0/3.2.2) [2.0/3.2.2] 46 | * [Part 3: Raising a 404 error](https://github.com/consideratecode/django-tutorial-step-by-step/tree/2.0/3.3.1) [2.0/3.3.1] 47 | * [Part 3: Raising a 404 error - A shortcut: get_object_or_404()](https://github.com/consideratecode/django-tutorial-step-by-step/tree/2.0/3.3.2) [2.0/3.3.2] 48 | * [Part 3: Use the template system](https://github.com/consideratecode/django-tutorial-step-by-step/tree/2.0/3.4) [2.0/3.4] 49 | * [Part 3: Removing hardcoded URLs in templates](https://github.com/consideratecode/django-tutorial-step-by-step/tree/2.0/3.5) [2.0/3.5] 50 | * [Part 3: Namespacing URL names](https://github.com/consideratecode/django-tutorial-step-by-step/tree/2.0/3.6) [2.0/3.6] 51 | 52 | Part 4 53 | * [Part 4: Write a simple form](https://github.com/consideratecode/django-tutorial-step-by-step/tree/2.0/4.1.1) [2.0/4.1.1] 54 | * [Part 4: Write a simple form - results view](https://github.com/consideratecode/django-tutorial-step-by-step/tree/2.0/4.1.2) [2.0/4.1.2] 55 | * [Part 4: Use generic views: Less code is better](https://github.com/consideratecode/django-tutorial-step-by-step/tree/2.0/4.2) [2.0/4.2] 56 | 57 | Part 5: 58 | * [Part 5: Writing our first test - Create a test to expose the bug](https://github.com/consideratecode/django-tutorial-step-by-step/tree/2.0/5.1.1) [2.0/5.1.1] 59 | * [Part 5: Writing our first test - Fixing the bug](https://github.com/consideratecode/django-tutorial-step-by-step/tree/2.0/5.1.2) [2.0/5.1.2] 60 | * [Part 5: Writing our first test - More comprehensive tests](https://github.com/consideratecode/django-tutorial-step-by-step/tree/2.0/5.1.3) [2.0/5.1.3] 61 | * [Part 5: Test a view - Improving our view](https://github.com/consideratecode/django-tutorial-step-by-step/tree/2.0/5.2.1) [2.0/5.2.1] 62 | * [Part 5: Test a view - Testing our new view](https://github.com/consideratecode/django-tutorial-step-by-step/tree/2.0/5.2.2) [2.0/5.2.2] 63 | * [Part 5: Test a view - Testing the DetailView](https://github.com/consideratecode/django-tutorial-step-by-step/tree/2.0/5.2.3) [2.0/5.2.3] 64 | 65 | Part 6: 66 | * [Part 6: Customize your app’s look and feel](https://github.com/consideratecode/django-tutorial-step-by-step/tree/2.0/6.1) [2.0/6.1] 67 | * [Part 6: Adding a background-image](https://github.com/consideratecode/django-tutorial-step-by-step/tree/2.0/6.2) [2.0/6.2] 68 | 69 | Part 7: 70 | * [Part 7: Customize the admin form - reordering the fields](https://github.com/consideratecode/django-tutorial-step-by-step/tree/2.0/7.1.1) [2.0/7.1.1] 71 | * [Part 7: Customize the admin form - split the form](https://github.com/consideratecode/django-tutorial-step-by-step/tree/2.0/7.1.2) [2.0/7.1.2] 72 | * [Part 7: Adding related objects - register Choice with the admin](https://github.com/consideratecode/django-tutorial-step-by-step/tree/2.0/7.2.1) [2.0/7.2.1] 73 | * [Part 7: Adding related objects - remove Choice from admin, add inline](https://github.com/consideratecode/django-tutorial-step-by-step/tree/2.0/7.2.2) [2.0/7.2.2] 74 | * [Part 7: Adding related objects - use TabularInline (instead of StackedInline)](https://github.com/consideratecode/django-tutorial-step-by-step/tree/2.0/7.2.3) [2.0/7.2.3] 75 | * [Part 7: Customize the admin change list - use list_display option](https://github.com/consideratecode/django-tutorial-step-by-step/tree/2.0/7.3.1) [2.0/7.3.1] 76 | * [Part 7: Customize the admin change list - include was_published_recently()](https://github.com/consideratecode/django-tutorial-step-by-step/tree/2.0/7.3.2) [2.0/7.3.2] 77 | * [Part 7: Customize the admin change list - add attributes to was_published_recently()](https://github.com/consideratecode/django-tutorial-step-by-step/tree/2.0/7.3.3) [2.0/7.3.3] 78 | * [Part 7: Customize the admin change list - add list_filter](https://github.com/consideratecode/django-tutorial-step-by-step/tree/2.0/7.3.4) [2.0/7.3.4] 79 | * [Part 7: Customize the admin change list - add search_fields](https://github.com/consideratecode/django-tutorial-step-by-step/tree/2.0/7.3.5) [2.0/7.3.5] 80 | * [Part 7: Customize the admin look and feel - Customizing your project’s templates](https://github.com/consideratecode/django-tutorial-step-by-step/tree/2.0/7.4) [2.0/7.4] 81 | 82 | If you want to check out the code at a specific step, simply checkout the tag. 83 | E.g. if you are stook in "Part 6: Customize your app’s look and feel", do the 84 | following: 85 | 86 | $ git clone git@github.com:consideratecode/django-tutorial-step-by-step.git 87 | $ cd django-tutorial-step-by-step 88 | $ git checkout 2.0/6.1 89 | 90 | This repository does intentionally not contain: 91 | 92 | * `__pycache__` directories containing Python bytecode 93 | * the file `db.sqlite3` containing the SQLite database 94 | 95 | 96 | 97 | Contributing 98 | ------------ 99 | If you find any mistakes, open an issue or create a pull request. If you are 100 | unsure how to do that, drop me an email. 101 | 102 | My goal is to stay true to the idea of one commit per step in the tutorial. 103 | This means that I will probably have to change tags and branch names if 104 | any changes are made. 105 | 106 | 107 | Author 108 | ------ 109 | Daniel Hepper 110 | 111 | 112 | License and Copyright 113 | --------------------- 114 | The code in this repository originates from the Django tutorial, released 115 | as part of Django under the Modified BSD License by the Django Software 116 | Foundation and individual contributors. My original contribution to this 117 | repository is essentionally this README. For the full terms, see LICENSE. 118 | 119 | The name "Django" is a registered trademark of the Django Software Foundation. 120 | Please note that any references to the official Django tutorial 121 | are nominative and should not imply affiliation with or endorsement by the 122 | Django Software Foundation. 123 | --------------------------------------------------------------------------------