Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cb8aeea
commit 39d0956
Showing
11 changed files
with
121 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from polls.models import Poll, Choice | ||
from django.contrib import admin | ||
|
||
class ChoiceInline(admin.TabularInline): | ||
model = Choice | ||
extra = 3 | ||
|
||
class PollAdmin(admin.ModelAdmin): | ||
list_display = ('question', 'pub_date', 'was_published_recently') | ||
fieldsets = [ | ||
(None, | ||
{'fields': ['question']}), | ||
('Date information', | ||
{'fields': ['pub_date'], 'classes': ['collapse']}), | ||
] | ||
list_filter = ['pub_date'] | ||
search_fields = ['question'] | ||
date_hierarchy = 'pub_date' | ||
inlines = [ChoiceInline] | ||
|
||
admin.site.register(Poll, PollAdmin) |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<h1>{{ poll.question }}</h1> | ||
|
||
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %} | ||
|
||
<form action="/polls/{{ poll.id }}/vote/" method="post"> | ||
{% csrf_token %} | ||
{% for choice in poll.choice_set.all %} | ||
<input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" /> | ||
<label for="choice{{ forloop.counter }}">{{ choice.choice }}</label><br /> | ||
{% endfor %} | ||
<input type="submit" value="Vote" /> | ||
</form> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{% if latest_poll_list %} | ||
<ul> | ||
{% for poll in latest_poll_list %} | ||
<li><a href="/polls/{{ poll.id }}/">{{ poll.question }}</a></li> | ||
{% endfor %} | ||
</ul> | ||
{% else %} | ||
<p>No polls are available.</p> | ||
{% endif %} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<h1>{{ poll.question }}</h1> | ||
|
||
<ul> | ||
{% for choice in poll.choice_set.all %} | ||
<li>{{ choice.choice }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li> | ||
{% endfor %} | ||
</ul> | ||
|
||
<a href="/polls/{{ poll.id }}/">Vote again?</a> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from django.conf.urls import patterns, include, url | ||
from django.views.generic import DetailView, ListView | ||
from polls.models import Poll | ||
|
||
urlpatterns = patterns('', | ||
url(r'^$', | ||
ListView.as_view( | ||
queryset=Poll.objects.order_by('-pub_date')[:5], | ||
context_object_name='latest_poll_list', | ||
template_name='polls/index.html')), | ||
url(r'^(?P<pk>\d+)/$', | ||
DetailView.as_view( | ||
model=Poll, | ||
template_name='polls/detail.html')), | ||
url(r'^(?P<pk>\d+)/results/$', | ||
DetailView.as_view( | ||
model=Poll, | ||
template_name='polls/results.html'), | ||
name='poll_results'), | ||
url(r'^(?P<poll_id>\d+)/vote/$', 'polls.views.vote'), | ||
) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,38 @@ | ||
# Create your views here. | ||
from django.shortcuts import get_object_or_404, render_to_response | ||
from django.http import HttpResponseRedirect, HttpResponse | ||
from django.core.urlresolvers import reverse | ||
from django.template import RequestContext | ||
|
||
from polls.models import Choice, Poll | ||
|
||
# def index(request): | ||
# latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5] | ||
# return render_to_response('polls/index.html', | ||
# {'latest_poll_list': latest_poll_list}) | ||
|
||
# def detail(request, poll_id): | ||
# p = get_object_or_404(Poll, pk=poll_id) | ||
# return render_to_response('polls/detail.html', {'poll': p}, | ||
# context_instance=RequestContext(request)) | ||
|
||
# def results(request, poll_id): | ||
# p = get_object_or_404(Poll, pk=poll_id) | ||
# return render_to_response('polls/results.html', {'poll': p}) | ||
|
||
def vote(request, poll_id): | ||
p = get_object_or_404(Poll, pk=poll_id) | ||
try: | ||
selected_choice = p.choice_set.get(pk=request.POST['choice']) | ||
except (KeyError, Choice.DoesNotExist): | ||
# Redisplay the poll voting form. | ||
return render_to_response('polls/detail.html', { | ||
'poll': p, | ||
'error_message': "You didn't select a choice.", | ||
}, context_instance=RequestContext(request)) | ||
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('poll_results', args=(p.id,))) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<h1>404 - page not found</h1> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<h1>500 - Server error, oopsie</h1> |
This file contains 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
This file contains 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