initial commit, up to part 4/5
This commit is contained in:
commit
ac1f869c94
BIN
mysite/db.sqlite3
Normal file
BIN
mysite/db.sqlite3
Normal file
Binary file not shown.
22
mysite/manage.py
Executable file
22
mysite/manage.py
Executable file
|
@ -0,0 +1,22 @@
|
|||
#!/usr/bin/env python
|
||||
import os
|
||||
import sys
|
||||
|
||||
if __name__ == "__main__":
|
||||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
|
||||
try:
|
||||
from django.core.management import execute_from_command_line
|
||||
except ImportError:
|
||||
# The above import may fail for some other reason. Ensure that the
|
||||
# issue is really that Django is missing to avoid masking other
|
||||
# exceptions on Python 2.
|
||||
try:
|
||||
import django
|
||||
except ImportError:
|
||||
raise ImportError(
|
||||
"Couldn't import Django. Are you sure it's installed and "
|
||||
"available on your PYTHONPATH environment variable? Did you "
|
||||
"forget to activate a virtual environment?"
|
||||
)
|
||||
raise
|
||||
execute_from_command_line(sys.argv)
|
0
mysite/mysite/__init__.py
Normal file
0
mysite/mysite/__init__.py
Normal file
BIN
mysite/mysite/__pycache__/__init__.cpython-37.pyc
Normal file
BIN
mysite/mysite/__pycache__/__init__.cpython-37.pyc
Normal file
Binary file not shown.
BIN
mysite/mysite/__pycache__/settings.cpython-37.pyc
Normal file
BIN
mysite/mysite/__pycache__/settings.cpython-37.pyc
Normal file
Binary file not shown.
BIN
mysite/mysite/__pycache__/urls.cpython-37.pyc
Normal file
BIN
mysite/mysite/__pycache__/urls.cpython-37.pyc
Normal file
Binary file not shown.
BIN
mysite/mysite/__pycache__/wsgi.cpython-37.pyc
Normal file
BIN
mysite/mysite/__pycache__/wsgi.cpython-37.pyc
Normal file
Binary file not shown.
121
mysite/mysite/settings.py
Normal file
121
mysite/mysite/settings.py
Normal file
|
@ -0,0 +1,121 @@
|
|||
"""
|
||||
Django settings for mysite project.
|
||||
|
||||
Generated by 'django-admin startproject' using Django 1.11.29.
|
||||
|
||||
For more information on this file, see
|
||||
https://docs.djangoproject.com/en/1.11/topics/settings/
|
||||
|
||||
For the full list of settings and their values, see
|
||||
https://docs.djangoproject.com/en/1.11/ref/settings/
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
|
||||
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
|
||||
# Quick-start development settings - unsuitable for production
|
||||
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/
|
||||
|
||||
# SECURITY WARNING: keep the secret key used in production secret!
|
||||
SECRET_KEY = 'm2h0wcn#80^c9*(+62i--zwft1c_4h0+8q#yte&9vu7s6l67!('
|
||||
|
||||
# SECURITY WARNING: don't run with debug turned on in production!
|
||||
DEBUG = True
|
||||
|
||||
ALLOWED_HOSTS = []
|
||||
|
||||
|
||||
# Application definition
|
||||
|
||||
INSTALLED_APPS = [
|
||||
'polls.apps.PollsConfig',
|
||||
'django.contrib.admin',
|
||||
'django.contrib.auth',
|
||||
'django.contrib.contenttypes',
|
||||
'django.contrib.sessions',
|
||||
'django.contrib.messages',
|
||||
'django.contrib.staticfiles',
|
||||
]
|
||||
|
||||
MIDDLEWARE = [
|
||||
'django.middleware.security.SecurityMiddleware',
|
||||
'django.contrib.sessions.middleware.SessionMiddleware',
|
||||
'django.middleware.common.CommonMiddleware',
|
||||
'django.middleware.csrf.CsrfViewMiddleware',
|
||||
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
||||
'django.contrib.messages.middleware.MessageMiddleware',
|
||||
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
||||
]
|
||||
|
||||
ROOT_URLCONF = 'mysite.urls'
|
||||
|
||||
TEMPLATES = [
|
||||
{
|
||||
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
||||
'DIRS': [],
|
||||
'APP_DIRS': True,
|
||||
'OPTIONS': {
|
||||
'context_processors': [
|
||||
'django.template.context_processors.debug',
|
||||
'django.template.context_processors.request',
|
||||
'django.contrib.auth.context_processors.auth',
|
||||
'django.contrib.messages.context_processors.messages',
|
||||
],
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
WSGI_APPLICATION = 'mysite.wsgi.application'
|
||||
|
||||
|
||||
# Database
|
||||
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases
|
||||
|
||||
DATABASES = {
|
||||
'default': {
|
||||
'ENGINE': 'django.db.backends.sqlite3',
|
||||
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
# Password validation
|
||||
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators
|
||||
|
||||
AUTH_PASSWORD_VALIDATORS = [
|
||||
{
|
||||
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
|
||||
},
|
||||
{
|
||||
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
|
||||
},
|
||||
{
|
||||
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
|
||||
},
|
||||
{
|
||||
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
# Internationalization
|
||||
# https://docs.djangoproject.com/en/1.11/topics/i18n/
|
||||
|
||||
LANGUAGE_CODE = 'en-us'
|
||||
|
||||
TIME_ZONE = 'UTC'
|
||||
|
||||
USE_I18N = True
|
||||
|
||||
USE_L10N = True
|
||||
|
||||
USE_TZ = True
|
||||
|
||||
|
||||
# Static files (CSS, JavaScript, Images)
|
||||
# https://docs.djangoproject.com/en/1.11/howto/static-files/
|
||||
|
||||
STATIC_URL = '/static/'
|
22
mysite/mysite/urls.py
Normal file
22
mysite/mysite/urls.py
Normal file
|
@ -0,0 +1,22 @@
|
|||
"""mysite URL Configuration
|
||||
|
||||
The `urlpatterns` list routes URLs to views. For more information please see:
|
||||
https://docs.djangoproject.com/en/1.11/topics/http/urls/
|
||||
Examples:
|
||||
Function views
|
||||
1. Add an import: from my_app import views
|
||||
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
|
||||
Class-based views
|
||||
1. Add an import: from other_app.views import Home
|
||||
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
|
||||
Including another URLconf
|
||||
1. Import the include() function: from django.conf.urls import url, include
|
||||
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
|
||||
"""
|
||||
from django.conf.urls import url, include
|
||||
from django.contrib import admin
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^polls/', include('polls.urls')),
|
||||
url(r'^admin/', admin.site.urls),
|
||||
]
|
16
mysite/mysite/wsgi.py
Normal file
16
mysite/mysite/wsgi.py
Normal file
|
@ -0,0 +1,16 @@
|
|||
"""
|
||||
WSGI config for mysite project.
|
||||
|
||||
It exposes the WSGI callable as a module-level variable named ``application``.
|
||||
|
||||
For more information on this file, see
|
||||
https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
from django.core.wsgi import get_wsgi_application
|
||||
|
||||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
|
||||
|
||||
application = get_wsgi_application()
|
0
mysite/polls/__init__.py
Normal file
0
mysite/polls/__init__.py
Normal file
BIN
mysite/polls/__pycache__/__init__.cpython-37.pyc
Normal file
BIN
mysite/polls/__pycache__/__init__.cpython-37.pyc
Normal file
Binary file not shown.
BIN
mysite/polls/__pycache__/admin.cpython-37.pyc
Normal file
BIN
mysite/polls/__pycache__/admin.cpython-37.pyc
Normal file
Binary file not shown.
BIN
mysite/polls/__pycache__/apps.cpython-37.pyc
Normal file
BIN
mysite/polls/__pycache__/apps.cpython-37.pyc
Normal file
Binary file not shown.
BIN
mysite/polls/__pycache__/models.cpython-37.pyc
Normal file
BIN
mysite/polls/__pycache__/models.cpython-37.pyc
Normal file
Binary file not shown.
BIN
mysite/polls/__pycache__/urls.cpython-37.pyc
Normal file
BIN
mysite/polls/__pycache__/urls.cpython-37.pyc
Normal file
Binary file not shown.
BIN
mysite/polls/__pycache__/views.cpython-37.pyc
Normal file
BIN
mysite/polls/__pycache__/views.cpython-37.pyc
Normal file
Binary file not shown.
4
mysite/polls/admin.py
Normal file
4
mysite/polls/admin.py
Normal file
|
@ -0,0 +1,4 @@
|
|||
from django.contrib import admin
|
||||
from .models import Question
|
||||
# Register your models here.
|
||||
admin.site.register(Question)
|
5
mysite/polls/apps.py
Normal file
5
mysite/polls/apps.py
Normal file
|
@ -0,0 +1,5 @@
|
|||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class PollsConfig(AppConfig):
|
||||
name = 'polls'
|
32
mysite/polls/migrations/0001_initial.py
Normal file
32
mysite/polls/migrations/0001_initial.py
Normal file
|
@ -0,0 +1,32 @@
|
|||
# Generated by Django 3.0.7 on 2020-06-22 14:12
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Question',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('question_text', models.CharField(max_length=200)),
|
||||
('pub_date', models.DateTimeField(verbose_name='date published')),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='Choice',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('choice_text', models.CharField(max_length=200)),
|
||||
('votes', models.IntegerField(default=0)),
|
||||
('question', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='polls.Question')),
|
||||
],
|
||||
),
|
||||
]
|
0
mysite/polls/migrations/__init__.py
Normal file
0
mysite/polls/migrations/__init__.py
Normal file
BIN
mysite/polls/migrations/__pycache__/0001_initial.cpython-37.pyc
Normal file
BIN
mysite/polls/migrations/__pycache__/0001_initial.cpython-37.pyc
Normal file
Binary file not shown.
BIN
mysite/polls/migrations/__pycache__/__init__.cpython-37.pyc
Normal file
BIN
mysite/polls/migrations/__pycache__/__init__.cpython-37.pyc
Normal file
Binary file not shown.
24
mysite/polls/models.py
Normal file
24
mysite/polls/models.py
Normal file
|
@ -0,0 +1,24 @@
|
|||
from django.db import models
|
||||
import datetime
|
||||
from django.utils import timezone
|
||||
|
||||
# Create your models here.
|
||||
class Question(models.Model):
|
||||
question_text = models.CharField(max_length=200)
|
||||
pub_date = models.DateTimeField('date published')
|
||||
|
||||
def __str__(self):
|
||||
return self.question_text
|
||||
def wasRecent(self):
|
||||
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
|
||||
|
||||
class Choice(models.Model):
|
||||
question = models.ForeignKey(Question, on_delete=models.CASCADE)
|
||||
choice_text = models.CharField(max_length=200)
|
||||
votes = models.IntegerField(default=0)
|
||||
|
||||
def __str__(self):
|
||||
return self.choice_text
|
||||
|
||||
|
||||
|
13
mysite/polls/templates/polls/detail.html
Normal file
13
mysite/polls/templates/polls/detail.html
Normal file
|
@ -0,0 +1,13 @@
|
|||
<h1>{{ question }}</h1>
|
||||
|
||||
{% if error_message %}<p><strong>{{ error_message }}</strong></p> {% endif %}
|
||||
|
||||
<form action="{% url 'polls:vote' question.id %}" method="post">
|
||||
{% csrf_token %}
|
||||
{% for choice in question.choice_set.all %}
|
||||
<input type="radio" name="choice" id="choice {{ forloop.counter }}" value="{{ choice.id }}">
|
||||
<label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label>
|
||||
<br>
|
||||
{% endfor %}
|
||||
<input type="submit" value="Vote">
|
||||
</form>
|
9
mysite/polls/templates/polls/index.html
Normal file
9
mysite/polls/templates/polls/index.html
Normal file
|
@ -0,0 +1,9 @@
|
|||
{% if question_list %}
|
||||
<ul>
|
||||
{% for question in question_list %}
|
||||
<li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% else %}
|
||||
<p> No Polls available. </p>
|
||||
{% endif %}
|
9
mysite/polls/templates/polls/result.html
Normal file
9
mysite/polls/templates/polls/result.html
Normal file
|
@ -0,0 +1,9 @@
|
|||
<h1>{{ question }}</h1>
|
||||
|
||||
<ul>
|
||||
{% for choice in question.choice_set.all %}
|
||||
<li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{choice.votes|pluralize}}</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
|
||||
<a href="{% url 'polls:detail' question.id %}">Vote again?</a>
|
3
mysite/polls/tests.py
Normal file
3
mysite/polls/tests.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
11
mysite/polls/urls.py
Normal file
11
mysite/polls/urls.py
Normal file
|
@ -0,0 +1,11 @@
|
|||
from django.urls import path
|
||||
|
||||
from . import views
|
||||
|
||||
app_name = 'polls'
|
||||
urlpatterns = [
|
||||
path('', views.index, name='index'),
|
||||
path('<int:question_id>/', views.detail, name='detail'),
|
||||
path('<int:question_id>/results/', views.result, name='result'),
|
||||
path('<int:question_id>/vote/', views.vote, name='vote')
|
||||
]
|
31
mysite/polls/views.py
Normal file
31
mysite/polls/views.py
Normal file
|
@ -0,0 +1,31 @@
|
|||
from django.shortcuts import render, get_object_or_404
|
||||
from django.http import HttpResponse, Http404, HttpResponseRedirect
|
||||
from django.template import loader
|
||||
from django.urls import reverse
|
||||
from .models import Question
|
||||
|
||||
# Create your views here.
|
||||
def index(request):
|
||||
question_list = Question.objects.order_by('-pub_date')[:5]
|
||||
context = { 'question_list': question_list }
|
||||
return render(request, 'polls/index.html', context)
|
||||
|
||||
def detail(request, question_id):
|
||||
question = get_object_or_404(Question, pk=question_id)
|
||||
return render(request, 'polls/detail.html', {'question': question})
|
||||
|
||||
def result(request, question_id):
|
||||
question = get_object_or_404(Question, pk=question_id)
|
||||
return render(request, 'polls/result.html', {'question': question})
|
||||
|
||||
def vote(request, question_id):
|
||||
question = get_object_or_404(Question, pk=question_id)
|
||||
try:
|
||||
selected_choice = question.choice_set.get(pk=request.POST['choice'])
|
||||
except(KeyError, choice.DoesNotExist):
|
||||
return render(request, 'polls/detail.html', {"question": question, "error_message": "You didn't select a choice",})
|
||||
else:
|
||||
selected_choice.votes += 1
|
||||
selected_choice.save()
|
||||
return HttpResponseRedirect(reverse('polls:result', args=(question.id, )))
|
||||
|
Loading…
Reference in a new issue