· 7 years ago · May 26, 2018, 02:38 AM
1from django.http import HttpResponseRedirect
2from django.shortcuts import get_object_or_404, render
3from django.urls import reverse
4from django.views import generic
5
6from .models import Choice, Question
7
8
9class IndexView(generic.ListView):
10 template_name = 'polls/index.html'
11 context_object_name = 'latest_question_list'
12
13 def get_queryset(self):
14 """Return the last five published questions."""
15 return Question.objects.order_by('-pub_date')[:5]
16
17
18class DetailView(generic.DetailView):
19 model = Question
20 template_name = 'polls/detail.html'
21
22
23class ResultsView(generic.DetailView):
24 model = Question
25 template_name = 'polls/results.html'
26
27
28def vote(request, question_id):
29 ... # same as above, no changes needed.
30
31from django.contrib import admin
32
33from .models import Question
34
35admin.site.register(Question)
36
37from django.urls import path
38
39from . import views
40
41app_name = 'polls'
42urlpatterns = [
43 path('', views.IndexView.as_view(), name='index'),
44 path('<int:pk>/', views.DetailView.as_view(), name='detail'),
45 path('<int:pk>/results/', views.ResultsView.as_view(), name='results'),
46 path('<int:question_id>/vote/', views.vote, name='vote'),
47]
48
49<h1>{{ question.question_text }}</h1>
50<ul>
51{% for choice in question.choice_set.all %}
52 <li>{{ choice.choice_text }}</li>
53{% endfor %}
54</ul>
55
56<h1>{{ question.question_text }}</h1>
57
58{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
59
60<form action="{% url 'polls:vote' question.id %}" method="post">
61{% csrf_token %}
62{% for choice in question.choice_set.all %}
63 <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" />
64 <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br />
65{% endfor %}
66<input type="submit" value="Vote" />
67</form>
68
69<h1>{{ question.question_text }}</h1>
70
71<ul>
72{% for choice in question.choice_set.all %}
73 <li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li>
74{% endfor %}
75</ul>
76
77<a href="{% url 'polls:detail' question.id %}">Vote again?</a>
78
79{% if latest_question_list %}
80 <ul>
81 {% for question in latest_question_list %}
82 <li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
83 {% endfor %}
84 </ul>
85{% else %}
86 <p>No polls are available.</p>
87{% endif %}
88
89"""
90Django settings for mysite3 project.
91
92Generated by 'django-admin startproject' using Django 2.0.
93
94For more information on this file, see
95https://docs.djangoproject.com/en/2.0/topics/settings/
96
97For the full list of settings and their values, see
98https://docs.djangoproject.com/en/2.0/ref/settings/
99"""
100
101import os
102
103# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
104BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
105
106
107# Quick-start development settings - unsuitable for production
108# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/
109
110# SECURITY WARNING: keep the secret key used in production secret!
111SECRET_KEY = '*8a@se$l_8v%(-xyz=!!=jgvyjyn611zqw(dqroejvkk^o9%co'
112
113# SECURITY WARNING: don't run with debug turned on in production!
114DEBUG = True
115
116ALLOWED_HOSTS = []
117
118
119# Application definition
120
121INSTALLED_APPS = [
122 'django.contrib.admin',
123 'django.contrib.auth',
124 'django.contrib.contenttypes',
125 'django.contrib.sessions',
126 'django.contrib.messages',
127 'django.contrib.staticfiles',
128 'polls.apps.PollsConfig',
129]
130
131MIDDLEWARE = [
132 'django.middleware.security.SecurityMiddleware',
133 'django.contrib.sessions.middleware.SessionMiddleware',
134 'django.middleware.common.CommonMiddleware',
135 'django.middleware.csrf.CsrfViewMiddleware',
136 'django.contrib.auth.middleware.AuthenticationMiddleware',
137 'django.contrib.messages.middleware.MessageMiddleware',
138 'django.middleware.clickjacking.XFrameOptionsMiddleware',
139]
140
141ROOT_URLCONF = 'mysite3.urls'
142
143TEMPLATES = [
144 {
145 'BACKEND': 'django.template.backends.django.DjangoTemplates',
146 'DIRS': [],
147 'APP_DIRS': True,
148 'OPTIONS': {
149 'context_processors': [
150 'django.template.context_processors.debug',
151 'django.template.context_processors.request',
152 'django.contrib.auth.context_processors.auth',
153 'django.contrib.messages.context_processors.messages',
154 ],
155 },
156 },
157]
158
159WSGI_APPLICATION = 'mysite3.wsgi.application'
160
161
162# Database
163# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
164
165DATABASES = {
166 'default': {
167 'ENGINE': 'django.db.backends.sqlite3',
168 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
169 }
170}
171
172
173# Password validation
174# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators
175
176AUTH_PASSWORD_VALIDATORS = [
177 {
178 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
179 },
180 {
181 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
182 },
183 {
184 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
185 },
186 {
187 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
188 },
189]
190
191
192# Internationalization
193# https://docs.djangoproject.com/en/2.0/topics/i18n/
194
195LANGUAGE_CODE = 'en-us'
196
197TIME_ZONE = 'UTC'
198
199USE_I18N = True
200
201USE_L10N = True
202
203USE_TZ = True
204
205
206# Static files (CSS, JavaScript, Images)
207# https://docs.djangoproject.com/en/2.0/howto/static-files/
208
209STATIC_URL = '/static/'