Up through tutorial part 3, the first bit
This commit is contained in:
parent
11d4baf648
commit
a5ce30aeeb
@ -1,3 +1,5 @@
|
|||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
|
|
||||||
# Register your models here.
|
from .models import Question
|
||||||
|
|
||||||
|
admin.site.register(Question)
|
||||||
|
@ -2,5 +2,5 @@ from django.apps import AppConfig
|
|||||||
|
|
||||||
|
|
||||||
class PollsConfig(AppConfig):
|
class PollsConfig(AppConfig):
|
||||||
default_auto_field = 'django.db.models.BigAutoField'
|
default_auto_field = "django.db.models.BigAutoField"
|
||||||
name = 'polls'
|
name = "polls"
|
||||||
|
@ -5,28 +5,47 @@ from django.db import migrations, models
|
|||||||
|
|
||||||
|
|
||||||
class Migration(migrations.Migration):
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
initial = True
|
initial = True
|
||||||
|
|
||||||
dependencies = [
|
dependencies = []
|
||||||
]
|
|
||||||
|
|
||||||
operations = [
|
operations = [
|
||||||
migrations.CreateModel(
|
migrations.CreateModel(
|
||||||
name='Question',
|
name="Question",
|
||||||
fields=[
|
fields=[
|
||||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
(
|
||||||
('question_text', models.CharField(max_length=200)),
|
"id",
|
||||||
('pub_date', models.DateTimeField(verbose_name='date published')),
|
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(
|
migrations.CreateModel(
|
||||||
name='Choice',
|
name="Choice",
|
||||||
fields=[
|
fields=[
|
||||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
(
|
||||||
('choice_text', models.CharField(max_length=200)),
|
"id",
|
||||||
('votes', models.IntegerField(default=0)),
|
models.BigAutoField(
|
||||||
('question', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='polls.question')),
|
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"
|
||||||
|
),
|
||||||
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
@ -1,12 +1,24 @@
|
|||||||
|
import datetime
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
from django.utils import timezone
|
||||||
|
|
||||||
|
|
||||||
class Question(models.Model):
|
class Question(models.Model):
|
||||||
question_text = models.CharField(max_length=200)
|
question_text = models.CharField(max_length=200)
|
||||||
pub_date = models.DateTimeField("date published")
|
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):
|
class Choice(models.Model):
|
||||||
question = models.ForeignKey(Question, on_delete=models.CASCADE)
|
question = models.ForeignKey(Question, on_delete=models.CASCADE)
|
||||||
choice_text = models.CharField(max_length=200)
|
choice_text = models.CharField(max_length=200)
|
||||||
votes = models.IntegerField(default=0)
|
votes = models.IntegerField(default=0)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.choice_text
|
||||||
|
@ -5,4 +5,7 @@ from . import views
|
|||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path("", views.index, name="index"),
|
path("", views.index, name="index"),
|
||||||
|
path("<int:question_id>/", views.detail, name="detail"),
|
||||||
|
path("<int:question_id>/results/", views.results, name="results"),
|
||||||
|
path("<int:question_id>/vote/", views.vote, name="vote"),
|
||||||
]
|
]
|
||||||
|
@ -1,6 +1,26 @@
|
|||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
|
|
||||||
|
from .models import Question
|
||||||
|
|
||||||
def index(request):
|
|
||||||
|
def index(request) -> HttpResponse:
|
||||||
|
latest_question_list = Question.objects.order_by("-pub_date")[:5]
|
||||||
|
output = ", ".join([q.question_text for q in latest_question_list])
|
||||||
|
return HttpResponse(output.encode())
|
||||||
|
|
||||||
|
|
||||||
|
def detail(request, question_id) -> HttpResponse:
|
||||||
_ = request
|
_ = request
|
||||||
return HttpResponse(b"Hello, world. You're at the polls index.")
|
return HttpResponse(f"You're looking at question {question_id}.".encode())
|
||||||
|
|
||||||
|
|
||||||
|
def results(request, question_id) -> HttpResponse:
|
||||||
|
_ = request
|
||||||
|
return HttpResponse(
|
||||||
|
f"You're looking at the results of question {question_id}".encode()
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def vote(request, question_id) -> HttpResponse:
|
||||||
|
_ = request
|
||||||
|
return HttpResponse(f"You're voting on question {question_id}".encode())
|
||||||
|
@ -1,2 +1,3 @@
|
|||||||
Django
|
Django
|
||||||
|
django-stubs
|
||||||
ruff
|
ruff
|
||||||
|
Loading…
Reference in New Issue
Block a user