Django poll thing
This commit is contained in:
0
djtut/mysite/polls/__init__.py
Normal file
0
djtut/mysite/polls/__init__.py
Normal file
5
djtut/mysite/polls/admin.py
Normal file
5
djtut/mysite/polls/admin.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from django.contrib import admin
|
||||
|
||||
from .models import Question
|
||||
|
||||
admin.site.register(Question)
|
5
djtut/mysite/polls/apps.py
Normal file
5
djtut/mysite/polls/apps.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class PollsConfig(AppConfig):
|
||||
name = "polls"
|
38
djtut/mysite/polls/migrations/0001_initial.py
Normal file
38
djtut/mysite/polls/migrations/0001_initial.py
Normal file
@@ -0,0 +1,38 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.11.29 on 2020-03-14 01:39
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
operations = [
|
||||
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)),
|
||||
],
|
||||
),
|
||||
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.AddField(
|
||||
model_name='choice',
|
||||
name='question',
|
||||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='polls.Question'),
|
||||
),
|
||||
]
|
0
djtut/mysite/polls/migrations/__init__.py
Normal file
0
djtut/mysite/polls/migrations/__init__.py
Normal file
12
djtut/mysite/polls/models.py
Normal file
12
djtut/mysite/polls/models.py
Normal file
@@ -0,0 +1,12 @@
|
||||
from django.db import models
|
||||
|
||||
|
||||
class Question(models.Model):
|
||||
question_text = models.CharField(max_length=200)
|
||||
pub_date = models.DateTimeField("date published")
|
||||
|
||||
|
||||
class Choice(models.Model):
|
||||
question = models.ForeignKey(Question, on_delete=models.CASCADE)
|
||||
choice_text = models.CharField(max_length=200)
|
||||
votes = models.IntegerField(default=0)
|
12
djtut/mysite/polls/templates/polls/detail.html
Normal file
12
djtut/mysite/polls/templates/polls/detail.html
Normal file
@@ -0,0 +1,12 @@
|
||||
<h1>{{ question.question_text }}</h1>
|
||||
|
||||
{% if error_message %}<p><strong>{{ error_mmessage }}</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
djtut/mysite/polls/templates/polls/index.html
Normal file
9
djtut/mysite/polls/templates/polls/index.html
Normal file
@@ -0,0 +1,9 @@
|
||||
{% if latest_question_list %}
|
||||
<ul>
|
||||
{% for question in latest_question_list %}
|
||||
<li><a href="{% url "polls:detail" question.id %}">{{ question.question_text }}</a></li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% else %}
|
||||
<p>No polls are available.</p>
|
||||
{% endif %}
|
9
djtut/mysite/polls/templates/polls/results.html
Normal file
9
djtut/mysite/polls/templates/polls/results.html
Normal file
@@ -0,0 +1,9 @@
|
||||
<h1>{{ question.question_text }}</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
djtut/mysite/polls/tests.py
Normal file
3
djtut/mysite/polls/tests.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
11
djtut/mysite/polls/urls.py
Normal file
11
djtut/mysite/polls/urls.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from django.conf.urls import url
|
||||
|
||||
from . import views
|
||||
|
||||
app_name = "polls"
|
||||
urlpatterns = [
|
||||
url("^$", views.index, name="index"),
|
||||
url("^(?P<question_id>[0-9]+)/$", views.detail, name="detail"),
|
||||
url("^(?P<question_id>[0-9]+)/results/$", views.results, name="results"),
|
||||
url("^(?P<question_id>[0-9]+)/vote/$", views.vote, name="vote"),
|
||||
]
|
39
djtut/mysite/polls/views.py
Normal file
39
djtut/mysite/polls/views.py
Normal file
@@ -0,0 +1,39 @@
|
||||
from django.shortcuts import render, get_object_or_404
|
||||
from django.http import HttpResponseRedirect, HttpResponse
|
||||
from django.urls import reverse
|
||||
|
||||
from .models import Question
|
||||
|
||||
|
||||
def index(request):
|
||||
latest_question_list = Question.objects.order_by("-pub_date")[:5]
|
||||
context = {
|
||||
"latest_question_list": latest_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 results(request, question_id):
|
||||
question = get_object_or_404(Question, pk=question_id)
|
||||
return render(request, "polls/results.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/vote.html",
|
||||
{"question": question, "error_message": "You didn't select a choice."},
|
||||
)
|
||||
else:
|
||||
selected_choice.votes += 1
|
||||
selected_choice.save()
|
||||
return HttpResponseRedirect(reverse("polls:results", args=(question.id,)))
|
Reference in New Issue
Block a user