before send to remote
This commit is contained in:
0
polls/__init__.py
Normal file
0
polls/__init__.py
Normal file
BIN
polls/__pycache__/__init__.cpython-310.pyc
Normal file
BIN
polls/__pycache__/__init__.cpython-310.pyc
Normal file
Binary file not shown.
BIN
polls/__pycache__/__init__.cpython-38.pyc
Normal file
BIN
polls/__pycache__/__init__.cpython-38.pyc
Normal file
Binary file not shown.
BIN
polls/__pycache__/admin.cpython-310.pyc
Normal file
BIN
polls/__pycache__/admin.cpython-310.pyc
Normal file
Binary file not shown.
BIN
polls/__pycache__/admin.cpython-38.pyc
Normal file
BIN
polls/__pycache__/admin.cpython-38.pyc
Normal file
Binary file not shown.
BIN
polls/__pycache__/apps.cpython-310.pyc
Normal file
BIN
polls/__pycache__/apps.cpython-310.pyc
Normal file
Binary file not shown.
BIN
polls/__pycache__/apps.cpython-38.pyc
Normal file
BIN
polls/__pycache__/apps.cpython-38.pyc
Normal file
Binary file not shown.
BIN
polls/__pycache__/models.cpython-310.pyc
Normal file
BIN
polls/__pycache__/models.cpython-310.pyc
Normal file
Binary file not shown.
BIN
polls/__pycache__/models.cpython-38.pyc
Normal file
BIN
polls/__pycache__/models.cpython-38.pyc
Normal file
Binary file not shown.
BIN
polls/__pycache__/urls.cpython-310.pyc
Normal file
BIN
polls/__pycache__/urls.cpython-310.pyc
Normal file
Binary file not shown.
BIN
polls/__pycache__/urls.cpython-38.pyc
Normal file
BIN
polls/__pycache__/urls.cpython-38.pyc
Normal file
Binary file not shown.
BIN
polls/__pycache__/views.cpython-310.pyc
Normal file
BIN
polls/__pycache__/views.cpython-310.pyc
Normal file
Binary file not shown.
BIN
polls/__pycache__/views.cpython-38.pyc
Normal file
BIN
polls/__pycache__/views.cpython-38.pyc
Normal file
Binary file not shown.
9
polls/admin.py
Normal file
9
polls/admin.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
||||
|
||||
from .models import Question
|
||||
from .models import Choice
|
||||
|
||||
admin.site.register(Question)
|
||||
admin.site.register(Choice)
|
6
polls/apps.py
Normal file
6
polls/apps.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class PollsConfig(AppConfig):
|
||||
default_auto_field = "django.db.models.BigAutoField"
|
||||
name = "polls"
|
52
polls/migrations/0001_initial.py
Normal file
52
polls/migrations/0001_initial.py
Normal file
@@ -0,0 +1,52 @@
|
||||
# Generated by Django 4.1 on 2022-08-16 14:10
|
||||
|
||||
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.BigAutoField(
|
||||
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.BigAutoField(
|
||||
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
polls/migrations/__init__.py
Normal file
0
polls/migrations/__init__.py
Normal file
BIN
polls/migrations/__pycache__/0001_initial.cpython-310.pyc
Normal file
BIN
polls/migrations/__pycache__/0001_initial.cpython-310.pyc
Normal file
Binary file not shown.
BIN
polls/migrations/__pycache__/0001_initial.cpython-38.pyc
Normal file
BIN
polls/migrations/__pycache__/0001_initial.cpython-38.pyc
Normal file
Binary file not shown.
BIN
polls/migrations/__pycache__/__init__.cpython-310.pyc
Normal file
BIN
polls/migrations/__pycache__/__init__.cpython-310.pyc
Normal file
Binary file not shown.
BIN
polls/migrations/__pycache__/__init__.cpython-38.pyc
Normal file
BIN
polls/migrations/__pycache__/__init__.cpython-38.pyc
Normal file
Binary file not shown.
24
polls/models.py
Normal file
24
polls/models.py
Normal file
@@ -0,0 +1,24 @@
|
||||
import datetime
|
||||
|
||||
from django.db import models
|
||||
from django.utils import timezone
|
||||
|
||||
|
||||
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 was_published_recently(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
|
28
polls/templates/polls/detail.html
Normal file
28
polls/templates/polls/detail.html
Normal file
@@ -0,0 +1,28 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Django Polls Details</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<form action="{% url 'polls:vote' question.id %}" method="post">
|
||||
{% csrf_token %}
|
||||
<fieldset>
|
||||
<legend>
|
||||
<h1>{{ question.question_text }}</h1>
|
||||
</legend>
|
||||
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
|
||||
{% 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 %}
|
||||
</fieldset>
|
||||
<input type="submit" value="Vote" style="font-size:1.5rem; border-radius: 10px; margin-top: 20px; width: 200px; height: 50px; background-color: lightblue">
|
||||
</form>
|
||||
</body>
|
||||
|
||||
</html>
|
25
polls/templates/polls/index.html
Normal file
25
polls/templates/polls/index.html
Normal file
@@ -0,0 +1,25 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Django Polls</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
{% if latest_question_list %}
|
||||
<h1 style="color: darkblue">Пожалуйста, выберите вопрос:</h1>
|
||||
<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 %}
|
||||
</body>
|
||||
|
||||
</html>
|
25
polls/templates/polls/results.html
Normal file
25
polls/templates/polls/results.html
Normal file
@@ -0,0 +1,25 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Django Polls Results</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<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>
|
||||
</body>
|
||||
|
||||
</html>
|
3
polls/tests.py
Normal file
3
polls/tests.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
27
polls/urls.py
Normal file
27
polls/urls.py
Normal file
@@ -0,0 +1,27 @@
|
||||
|
||||
|
||||
from django.urls import path
|
||||
from . import views
|
||||
|
||||
app_name = 'polls'
|
||||
|
||||
# oldvariant
|
||||
urlpatterns = [
|
||||
# ex: /polls/
|
||||
path('', views.index, name='index'),
|
||||
# ex: /polls/5/
|
||||
path('<int:question_id>/', views.detail, name='detail'),
|
||||
# ex: /polls/5/results/
|
||||
path('<int:question_id>/results/', views.results, name='results'),
|
||||
# ex: /polls/5/vote/
|
||||
path('<int:question_id>/vote/', views.vote, name='vote'),
|
||||
]
|
||||
|
||||
# newvariant
|
||||
urlpatterns = [
|
||||
|
||||
path('', views.index, name='index'),
|
||||
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
|
||||
path('<int:pk>/results/', views.ResultsView.as_view(), name='results'),
|
||||
path('<int:question_id>/vote/', views.vote, name='vote'), # ex: /polls/5/vote/
|
||||
]
|
62
polls/views.py
Normal file
62
polls/views.py
Normal file
@@ -0,0 +1,62 @@
|
||||
# from django.shortcuts import render
|
||||
from django.http import HttpResponse, HttpResponseRedirect
|
||||
from django.shortcuts import get_object_or_404, render
|
||||
from django.urls import reverse
|
||||
from django.views import generic
|
||||
|
||||
# Create your views here.
|
||||
from .models import Question, Choice
|
||||
|
||||
|
||||
class IndexView(generic.ListView):
|
||||
template_name = 'polls/index.html'
|
||||
context_object_name = 'latest_question_list'
|
||||
|
||||
def get_queryset(self):
|
||||
"""Return the last five published questions."""
|
||||
return Question.objects.order_by('-pub_date')[:5]
|
||||
|
||||
|
||||
class DetailView(generic.DetailView):
|
||||
model = Question
|
||||
template_name = 'polls/detail.html'
|
||||
|
||||
|
||||
class ResultsView(generic.DetailView):
|
||||
model = Question
|
||||
template_name = 'polls/results.html'
|
||||
|
||||
|
||||
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):
|
||||
# Redisplay the question voting form.
|
||||
return render(request, 'polls/detail.html', {
|
||||
'question': question,
|
||||
'error_message': "You didn't select a choice.",
|
||||
})
|
||||
else:
|
||||
selected_choice.votes += 1
|
||||
selected_choice.save()
|
||||
# Always return an HttpResponseRedirect after successfully dealing
|
||||
# with POST data. This prevents data from being posted twice if a
|
||||
# user hits the Back button.
|
||||
return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))
|
Reference in New Issue
Block a user