This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.contrib import admin | |
from .models import Question | |
class QuestionAdmin(admin.ModelAdmin): | |
fields = ['pub_date', 'question_text'] | |
admin.site.register(Question, QuestionAdmin) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.utils import timezone | |
def get_queryset(self): | |
""" | |
Return the last five published questions (not including those set to be | |
published in the future). | |
""" | |
return Question.objects.filter( | |
pub_date__lte=timezone.now() | |
).order_by('-pub_date')[:5] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def create_question(question_text, days): | |
""" | |
Create a question with the given `question_text` and published the | |
given number of `days` offset to now (negative for questions published | |
in the past, positive for questions that have yet to be published). | |
""" | |
time = timezone.now() + datetime.timedelta(days=days) | |
return Question.objects.create(question_text=question_text, pub_date=time) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.test.utils import setup_test_environment | |
setup_test_environment() | |
from django.test import Client | |
client = Client() | |
response = client.get('/') | |
response.status_code | |
from django.urls import reverse |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def test_was_published_recently_with_old_question(self): | |
""" | |
was_published_recently() returns False for questions whose pub_date | |
is older than 1 day. | |
""" | |
time = timezone.now() - datetime.timedelta(days=1, seconds=1) | |
old_question = Question(pub_date=time) | |
self.assertIs(old_question.was_published_recently(), False) | |
def test_was_published_recently_with_recent_question(self): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def was_published_recently(self): | |
now = timezone.now() | |
return now - datetime.timedelta(days=1) <= self.pub_date <= now |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
from django.utils import timezone | |
from django.test import TestCase | |
from .models import Question | |
class QuestionModelTests(TestCase): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.shortcuts import get_object_or_404, render | |
from django.http import HttpResponseRedirect | |
from django.urls import reverse | |
from django.views import generic | |
from .models import Choice, Question | |
class IndexView(generic.ListView): | |
template_name = 'polls/index.html' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.urls import path | |
from . import views | |
app_name = 'polls' | |
urlpatterns = [ | |
path('', views.IndexView.as_view(), 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'), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |