Forgotten things with django tutorial
This commit is contained in:
@@ -47,7 +47,8 @@ DATABASES = {
|
||||
"HOST": "localhost",
|
||||
"PORT": "5432",
|
||||
"PASSWORD": "notsecurelol",
|
||||
}
|
||||
},
|
||||
"test": {"ENGINE": "django.db.backends.sqlite3", "NAME": "test-mysite.sqlite3",},
|
||||
}
|
||||
AUTH_PASSWORD_VALIDATORS = [
|
||||
{
|
||||
|
@@ -1,3 +1,6 @@
|
||||
import datetime
|
||||
|
||||
from django.utils import timezone
|
||||
from django.db import models
|
||||
|
||||
|
||||
@@ -5,6 +8,10 @@ class Question(models.Model):
|
||||
question_text = models.CharField(max_length=200)
|
||||
pub_date = models.DateTimeField("date published")
|
||||
|
||||
def was_published_recently(self):
|
||||
now = timezone.now()
|
||||
return now - datetime.timedelta(days=1) <= self.pub_date <= now
|
||||
|
||||
|
||||
class Choice(models.Model):
|
||||
question = models.ForeignKey(Question, on_delete=models.CASCADE)
|
||||
|
@@ -1,3 +1,11 @@
|
||||
from django.test import TestCase
|
||||
import datetime
|
||||
|
||||
# Create your tests here.
|
||||
from django.utils import timezone
|
||||
|
||||
from .models import Question
|
||||
|
||||
|
||||
def test_was_published_recently_with_future_question():
|
||||
time = timezone.now() + datetime.timedelta(days=30)
|
||||
future_question = Question(pub_date=time)
|
||||
assert not future_question.was_published_recently()
|
||||
|
@@ -4,8 +4,8 @@ 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("^$", views.IndexView.as_view(), name="index"),
|
||||
url("^(?P<pk>[0-9]+)/$", views.DetailView.as_view(), name="detail"),
|
||||
url("^(?P<pk>[0-9]+)/results/$", views.ResultsView.as_view(), name="results",),
|
||||
url("^(?P<question_id>[0-9]+)/vote/$", views.vote, name="vote"),
|
||||
]
|
||||
|
@@ -1,26 +1,27 @@
|
||||
from django.shortcuts import render, get_object_or_404
|
||||
from django.http import HttpResponseRedirect, HttpResponse
|
||||
from django.shortcuts import render, get_object_or_404
|
||||
from django.urls import reverse
|
||||
from django.views import generic
|
||||
|
||||
from .models import Question
|
||||
from .models import Choice, 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)
|
||||
class IndexView(generic.ListView):
|
||||
template_name = "polls/index.html"
|
||||
context_object_name = "latest_question_list"
|
||||
|
||||
def get_queryset(self):
|
||||
return Question.objects.order_by("-pub_date")[:5]
|
||||
|
||||
|
||||
def detail(request, question_id):
|
||||
question = get_object_or_404(Question, pk=question_id)
|
||||
return render(request, "polls/detail.html", {"question": question})
|
||||
class DetailView(generic.DetailView):
|
||||
model = Question
|
||||
template_name = "polls/detail.html"
|
||||
|
||||
|
||||
def results(request, question_id):
|
||||
question = get_object_or_404(Question, pk=question_id)
|
||||
return render(request, "polls/results.html", {"question": question})
|
||||
class ResultsView(generic.DetailView):
|
||||
model = Question
|
||||
template_name = "polls/results.html"
|
||||
|
||||
|
||||
def vote(request, question_id):
|
||||
|
2
djtut/mysite/pytest.ini
Normal file
2
djtut/mysite/pytest.ini
Normal file
@@ -0,0 +1,2 @@
|
||||
[pytest]
|
||||
DJANGO_SETTINGS_MODULE = mysite.settings
|
Reference in New Issue
Block a user