Skip to content

Commit

Permalink
finished up the example poll app
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewschoen committed Jul 18, 2012
1 parent cb8aeea commit 39d0956
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 3 deletions.
21 changes: 21 additions & 0 deletions demo/polls/admin.py
@@ -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)
5 changes: 4 additions & 1 deletion demo/polls/models.py
Expand Up @@ -9,14 +9,17 @@ class Poll(models.Model):

def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
was_published_recently.admin_order_field = 'pub_date'
was_published_recently.boolean = True
was_published_recently.short_description = 'Published recently?'

def __unicode__(self):
return self.question

class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice = models.CharField(max_length=200)
votes = models.IntegerField()
votes = models.IntegerField(default=0)

def __unicode__(self):
return self.choice
12 changes: 12 additions & 0 deletions demo/polls/templates/polls/detail.html
@@ -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>
9 changes: 9 additions & 0 deletions demo/polls/templates/polls/index.html
@@ -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 %}
9 changes: 9 additions & 0 deletions demo/polls/templates/polls/results.html
@@ -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>
21 changes: 21 additions & 0 deletions demo/polls/urls.py
@@ -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'),
)
39 changes: 38 additions & 1 deletion demo/polls/views.py
@@ -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,)))
1 change: 1 addition & 0 deletions demo/templates/404.html
@@ -0,0 +1 @@
<h1>404 - page not found</h1>
1 change: 1 addition & 0 deletions demo/templates/500.html
@@ -0,0 +1 @@
<h1>500 - Server error, oopsie</h1>
5 changes: 4 additions & 1 deletion demo/templates/base.html
@@ -1,10 +1,13 @@
{% load cms_tags sekizai_tags %}
{% load cms_tags sekizai_tags menu_tags %}
<html>
<head>
{% render_block "css" %}
</head>
<body>
{% cms_toolbar %}
<ul>
{% show_menu %}
</ul>
{% placeholder base_content %}
{% block base_content%}{% endblock %}
{% render_block "js" %}
Expand Down
1 change: 1 addition & 0 deletions demo/urls.py
Expand Up @@ -5,6 +5,7 @@
admin.autodiscover()

urlpatterns = patterns('',
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('cms.urls')),
)
Expand Down

0 comments on commit 39d0956

Please sign in to comment.