No polls available
16 | {% endif %} 17 | {% endblock %} -------------------------------------------------------------------------------- /pollster/templates/polls/results.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | {% block content %} 3 |8 | {{ error_message }} 9 |
10 | {% endif %} 11 | 12 | 23 | {% endblock %} -------------------------------------------------------------------------------- /pollster/polls/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 3.0.4 on 2020-03-21 06:24 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='Question', 17 | fields=[ 18 | ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 19 | ('question_text', models.CharField(max_length=200)), 20 | ('pub_date', models.DateTimeField(verbose_name='date published')), 21 | ], 22 | ), 23 | migrations.CreateModel( 24 | name='Choice', 25 | fields=[ 26 | ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 27 | ('choice_text', models.CharField(max_length=200)), 28 | ('votes', models.IntegerField(default=0)), 29 | ('question', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='polls.Question')), 30 | ], 31 | ), 32 | ] 33 | -------------------------------------------------------------------------------- /pollster/polls/views.py: -------------------------------------------------------------------------------- 1 | from django.template import loader 2 | from django.http import HttpResponse, HttpResponseRedirect 3 | from django.shortcuts import get_object_or_404, render 4 | from django.urls import reverse 5 | 6 | from .models import Question, Choice 7 | 8 | # Get questions and display them 9 | 10 | 11 | def index(request): 12 | latest_question_list = Question.objects.order_by('-pub_date')[:5] 13 | context = {'latest_question_list': latest_question_list} 14 | return render(request, 'polls/index.html', context) 15 | 16 | # Show specific question and choices 17 | 18 | 19 | def detail(request, question_id): 20 | try: 21 | question = Question.objects.get(pk=question_id) 22 | except Question.DoesNotExist: 23 | raise Http404("Question does not exist") 24 | return render(request, 'polls/detail.html', {'question': question}) 25 | 26 | # Get question and display results 27 | 28 | 29 | def results(request, question_id): 30 | question = get_object_or_404(Question, pk=question_id) 31 | return render(request, 'polls/results.html', {'question': question}) 32 | 33 | # Vote for a question choice 34 | 35 | 36 | def vote(request, question_id): 37 | # print(request.POST['choice']) 38 | question = get_object_or_404(Question, pk=question_id) 39 | try: 40 | selected_choice = question.choice_set.get(pk=request.POST['choice']) 41 | except (KeyError, Choice.DoesNotExist): 42 | # Redisplay the question voting form. 43 | return render(request, 'polls/detail.html', { 44 | 'question': question, 45 | 'error_message': "You didn't select a choice.", 46 | }) 47 | else: 48 | selected_choice.votes += 1 49 | selected_choice.save() 50 | # Always return an HttpResponseRedirect after successfully dealing 51 | # with POST data. This prevents data from being posted twice if a 52 | # user hits the Back button. 53 | return HttpResponseRedirect(reverse('polls:results', args=(question.id,))) 54 | -------------------------------------------------------------------------------- /pollster/pollster/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for pollster project. 3 | 4 | Generated by 'django-admin startproject' using Django 3.0.4. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/3.0/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/3.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/3.0/howto/deployment/checklist/ 21 | 22 | # SECURITY WARNING: keep the secret key used in production secret! 23 | SECRET_KEY = 'ayl0uawxbrh5-@unj4ky7@=mf8h_m_-y6c46-#(!f^w42(vx-w' 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 = 'pollster.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 = 'pollster.wsgi.application' 72 | 73 | 74 | # Database 75 | # https://docs.djangoproject.com/en/3.0/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/3.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/3.0/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/3.0/howto/static-files/ 120 | 121 | STATIC_URL = '/static/' 122 | -------------------------------------------------------------------------------- /Pipfile.lock: -------------------------------------------------------------------------------- 1 | { 2 | "_meta": { 3 | "hash": { 4 | "sha256": "956d91168def7a5b912b323f44e6b42dd7f6a075d3e69755a6878f63a275fdb8" 5 | }, 6 | "pipfile-spec": 6, 7 | "requires": { 8 | "python_version": "3.8" 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:3e4192eaec0758b99722f0b0666d5fbfaa713054d92e8de5b58ba84ec5ce696f", 22 | "sha256:c8f49dd3b42edcc51d09dd2eea8a92b3cfc987ff7e6486be734b4d0cbfd5d315" 23 | ], 24 | "version": "==3.2.5" 25 | }, 26 | "django": { 27 | "hashes": [ 28 | "sha256:50b781f6cbeb98f673aa76ed8e572a019a45e52bdd4ad09001072dfd91ab07c8", 29 | "sha256:89e451bfbb815280b137e33e454ddd56481fdaa6334054e6e031041ee1eda360" 30 | ], 31 | "index": "pypi", 32 | "version": "==3.0.4" 33 | }, 34 | "pytz": { 35 | "hashes": [ 36 | "sha256:1c557d7d0e871de1f5ccd5833f60fb2550652da6be2693c1e02300743d21500d", 37 | "sha256:b02c06db6cf09c12dd25137e563b31700d3b80fcc4ad23abb7a315f2789819be" 38 | ], 39 | "version": "==2019.3" 40 | }, 41 | "sqlparse": { 42 | "hashes": [ 43 | "sha256:022fb9c87b524d1f7862b3037e541f68597a730a8843245c349fc93e1643dc4e", 44 | "sha256:e162203737712307dfe78860cc56c8da8a852ab2ee33750e33aeadf38d12c548" 45 | ], 46 | "version": "==0.3.1" 47 | } 48 | }, 49 | "develop": { 50 | "astroid": { 51 | "hashes": [ 52 | "sha256:71ea07f44df9568a75d0f354c49143a4575d90645e9fead6dfb52c26a85ed13a", 53 | "sha256:840947ebfa8b58f318d42301cf8c0a20fd794a33b61cc4638e28e9e61ba32f42" 54 | ], 55 | "version": "==2.3.3" 56 | }, 57 | "autopep8": { 58 | "hashes": [ 59 | "sha256:0f592a0447acea0c2b0a9602be1e4e3d86db52badd2e3c84f0193bfd89fd3a43" 60 | ], 61 | "index": "pypi", 62 | "version": "==1.5" 63 | }, 64 | "colorama": { 65 | "hashes": [ 66 | "sha256:7d73d2a99753107a36ac6b455ee49046802e59d9d076ef8e47b61499fa29afff", 67 | "sha256:e96da0d330793e2cb9485e9ddfd918d456036c7149416295932478192f4436a1" 68 | ], 69 | "markers": "sys_platform == 'win32'", 70 | "version": "==0.4.3" 71 | }, 72 | "isort": { 73 | "hashes": [ 74 | "sha256:54da7e92468955c4fceacd0c86bd0ec997b0e1ee80d97f67c35a78b719dccab1", 75 | "sha256:6e811fcb295968434526407adb8796944f1988c5b65e8139058f2014cbe100fd" 76 | ], 77 | "version": "==4.3.21" 78 | }, 79 | "lazy-object-proxy": { 80 | "hashes": [ 81 | "sha256:0c4b206227a8097f05c4dbdd323c50edf81f15db3b8dc064d08c62d37e1a504d", 82 | "sha256:194d092e6f246b906e8f70884e620e459fc54db3259e60cf69a4d66c3fda3449", 83 | "sha256:1be7e4c9f96948003609aa6c974ae59830a6baecc5376c25c92d7d697e684c08", 84 | "sha256:4677f594e474c91da97f489fea5b7daa17b5517190899cf213697e48d3902f5a", 85 | "sha256:48dab84ebd4831077b150572aec802f303117c8cc5c871e182447281ebf3ac50", 86 | "sha256:5541cada25cd173702dbd99f8e22434105456314462326f06dba3e180f203dfd", 87 | "sha256:59f79fef100b09564bc2df42ea2d8d21a64fdcda64979c0fa3db7bdaabaf6239", 88 | "sha256:8d859b89baf8ef7f8bc6b00aa20316483d67f0b1cbf422f5b4dc56701c8f2ffb", 89 | "sha256:9254f4358b9b541e3441b007a0ea0764b9d056afdeafc1a5569eee1cc6c1b9ea", 90 | "sha256:9651375199045a358eb6741df3e02a651e0330be090b3bc79f6d0de31a80ec3e", 91 | "sha256:97bb5884f6f1cdce0099f86b907aa41c970c3c672ac8b9c8352789e103cf3156", 92 | "sha256:9b15f3f4c0f35727d3a0fba4b770b3c4ebbb1fa907dbcc046a1d2799f3edd142", 93 | "sha256:a2238e9d1bb71a56cd710611a1614d1194dc10a175c1e08d75e1a7bcc250d442", 94 | "sha256:a6ae12d08c0bf9909ce12385803a543bfe99b95fe01e752536a60af2b7797c62", 95 | "sha256:ca0a928a3ddbc5725be2dd1cf895ec0a254798915fb3a36af0964a0a4149e3db", 96 | "sha256:cb2c7c57005a6804ab66f106ceb8482da55f5314b7fcb06551db1edae4ad1531", 97 | "sha256:d74bb8693bf9cf75ac3b47a54d716bbb1a92648d5f781fc799347cfc95952383", 98 | "sha256:d945239a5639b3ff35b70a88c5f2f491913eb94871780ebfabb2568bd58afc5a", 99 | "sha256:eba7011090323c1dadf18b3b689845fd96a61ba0a1dfbd7f24b921398affc357", 100 | "sha256:efa1909120ce98bbb3777e8b6f92237f5d5c8ea6758efea36a473e1d38f7d3e4", 101 | "sha256:f3900e8a5de27447acbf900b4750b0ddfd7ec1ea7fbaf11dfa911141bc522af0" 102 | ], 103 | "version": "==1.4.3" 104 | }, 105 | "mccabe": { 106 | "hashes": [ 107 | "sha256:ab8a6258860da4b6677da4bd2fe5dc2c659cff31b3ee4f7f5d64e79735b80d42", 108 | "sha256:dd8d182285a0fe56bace7f45b5e7d1a6ebcbf524e8f3bd87eb0f125271b8831f" 109 | ], 110 | "version": "==0.6.1" 111 | }, 112 | "pycodestyle": { 113 | "hashes": [ 114 | "sha256:95a2219d12372f05704562a14ec30bc76b05a5b297b21a5dfe3f6fac3491ae56", 115 | "sha256:e40a936c9a450ad81df37f549d676d127b1b66000a6c500caa2b085bc0ca976c" 116 | ], 117 | "version": "==2.5.0" 118 | }, 119 | "pylint": { 120 | "hashes": [ 121 | "sha256:3db5468ad013380e987410a8d6956226963aed94ecb5f9d3a28acca6d9ac36cd", 122 | "sha256:886e6afc935ea2590b462664b161ca9a5e40168ea99e5300935f6591ad467df4" 123 | ], 124 | "index": "pypi", 125 | "version": "==2.4.4" 126 | }, 127 | "six": { 128 | "hashes": [ 129 | "sha256:236bdbdce46e6e6a3d61a337c0f8b763ca1e8717c03b369e87a7ec7ce1319c0a", 130 | "sha256:8f3cd2e254d8f793e7f3d6d9df77b92252b52637291d0f0da013c76ea2724b6c" 131 | ], 132 | "version": "==1.14.0" 133 | }, 134 | "wrapt": { 135 | "hashes": [ 136 | "sha256:565a021fd19419476b9362b05eeaa094178de64f8361e44468f9e9d7843901e1" 137 | ], 138 | "version": "==1.11.2" 139 | } 140 | } 141 | } 142 | --------------------------------------------------------------------------------