diff --git a/mysite/db.sqlite3 b/mysite/db.sqlite3 new file mode 100644 index 0000000..0e4c1a2 Binary files /dev/null and b/mysite/db.sqlite3 differ diff --git a/mysite/manage.py b/mysite/manage.py new file mode 100755 index 0000000..8a50ec0 --- /dev/null +++ b/mysite/manage.py @@ -0,0 +1,10 @@ +#!/usr/bin/env python +import os +import sys + +if __name__ == "__main__": + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings") + + from django.core.management import execute_from_command_line + + execute_from_command_line(sys.argv) diff --git a/mysite/mysite/__init__.py b/mysite/mysite/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/mysite/mysite/settings.py b/mysite/mysite/settings.py new file mode 100644 index 0000000..d7c795e --- /dev/null +++ b/mysite/mysite/settings.py @@ -0,0 +1,86 @@ +""" +Django settings for mysite project. + +For more information on this file, see +https://docs.djangoproject.com/en/1.7/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/1.7/ref/settings/ +""" + +# Build paths inside the project like this: os.path.join(BASE_DIR, ...) +import os +BASE_DIR = os.path.dirname(os.path.dirname(__file__)) + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = '(zw2ktyg4cq=x5#v+df58kgu##l(s%e890(jv0=bjkzh0%h2af' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +TEMPLATE_DEBUG = True + +ALLOWED_HOSTS = [] + + +# Application definition + +INSTALLED_APPS = ( + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + 'polls', +) + +MIDDLEWARE_CLASSES = ( + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +) + +ROOT_URLCONF = 'mysite.urls' + +WSGI_APPLICATION = 'mysite.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/1.7/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), + } +} + +# Internationalization +# https://docs.djangoproject.com/en/1.7/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_L10N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/1.7/howto/static-files/ + +STATIC_URL = '/static/' + +TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')] diff --git a/mysite/mysite/urls.py b/mysite/mysite/urls.py new file mode 100644 index 0000000..65fd802 --- /dev/null +++ b/mysite/mysite/urls.py @@ -0,0 +1,7 @@ +from django.conf.urls import patterns, include, url +from django.contrib import admin + +urlpatterns = patterns('', + url(r'^polls/', include('polls.urls', namespace="polls")), + url(r'^admin/', include(admin.site.urls)), +) \ No newline at end of file diff --git a/mysite/mysite/wsgi.py b/mysite/mysite/wsgi.py new file mode 100644 index 0000000..15c7d49 --- /dev/null +++ b/mysite/mysite/wsgi.py @@ -0,0 +1,14 @@ +""" +WSGI config for mysite project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/1.7/howto/deployment/wsgi/ +""" + +import os +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings") + +from django.core.wsgi import get_wsgi_application +application = get_wsgi_application() diff --git a/mysite/polls/__init__.py b/mysite/polls/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/mysite/polls/admin.py b/mysite/polls/admin.py new file mode 100644 index 0000000..3a875e1 --- /dev/null +++ b/mysite/polls/admin.py @@ -0,0 +1,21 @@ +from django.contrib import admin +from models import Question, Choice + + +class ChoiceInline(admin.TabularInline): + model = Choice + extra = 3 + + +class QuestionAdmin(admin.ModelAdmin): + list_display = ('question_text', 'pub_date', 'was_published_recently') + list_filter = ['pub_date'] + search_fields = ['question_text'] + fieldsets = [ + (None, {'fields': ['question_text']}), + ('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}), + ] + inlines = [ChoiceInline] + +admin.site.register(Question, QuestionAdmin) +admin.site.register(Choice) \ No newline at end of file diff --git a/mysite/polls/migrations/0001_initial.py b/mysite/polls/migrations/0001_initial.py new file mode 100644 index 0000000..f6837ef --- /dev/null +++ b/mysite/polls/migrations/0001_initial.py @@ -0,0 +1,41 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Choice', + fields=[ + ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), + ('choice_text', models.CharField(max_length=200)), + ('votes', models.IntegerField(default=0)), + ], + options={ + }, + bases=(models.Model,), + ), + migrations.CreateModel( + name='Question', + fields=[ + ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), + ('question_text', models.CharField(max_length=200)), + ('pub_date', models.DateTimeField(verbose_name=b'date published')), + ], + options={ + }, + bases=(models.Model,), + ), + migrations.AddField( + model_name='choice', + name='question', + field=models.ForeignKey(to='polls.Question'), + preserve_default=True, + ), + ] diff --git a/mysite/polls/migrations/__init__.py b/mysite/polls/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/mysite/polls/models.py b/mysite/polls/models.py new file mode 100644 index 0000000..a832ae2 --- /dev/null +++ b/mysite/polls/models.py @@ -0,0 +1,28 @@ +import datetime + +from django.db import models +from django.utils import timezone + + +class Question(models.Model): + question_text = models.CharField(max_length=200) + pub_date = models.DateTimeField('date published') + + 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 __str__(self): + return self.question_text + + +class Choice(models.Model): + question = models.ForeignKey(Question) + choice_text = models.CharField(max_length=200) + votes = models.IntegerField(default=0) + + def __str__(self): + return self.choice_text \ No newline at end of file diff --git a/mysite/polls/templates/polls/detail.html b/mysite/polls/templates/polls/detail.html new file mode 100644 index 0000000..2f36854 --- /dev/null +++ b/mysite/polls/templates/polls/detail.html @@ -0,0 +1,12 @@ +

{{ question.question_text }}

+ +{% if error_message %}

{{ error_message }}

{% endif %} + +
+{% csrf_token %} +{% for choice in question.choice_set.all %} + +
+{% endfor %} + +
\ No newline at end of file diff --git a/mysite/polls/templates/polls/index.html b/mysite/polls/templates/polls/index.html new file mode 100644 index 0000000..cc41400 --- /dev/null +++ b/mysite/polls/templates/polls/index.html @@ -0,0 +1,9 @@ +{% if latest_question_list %} + +{% else %} +

No polls are available.

+{% endif %} \ No newline at end of file diff --git a/mysite/polls/templates/polls/results.html b/mysite/polls/templates/polls/results.html new file mode 100644 index 0000000..f9b3666 --- /dev/null +++ b/mysite/polls/templates/polls/results.html @@ -0,0 +1,9 @@ +

{{ question.question_text }}

+ + + +Vote again? \ No newline at end of file diff --git a/mysite/polls/tests.py b/mysite/polls/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/mysite/polls/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/mysite/polls/urls.py b/mysite/polls/urls.py new file mode 100644 index 0000000..00b1f35 --- /dev/null +++ b/mysite/polls/urls.py @@ -0,0 +1,10 @@ +from django.conf.urls import patterns, url + +from polls import views + +urlpatterns = patterns('', + url(r'^$', views.IndexView.as_view(), name='index'), + url(r'^(?P\d+)/$', views.DetailView.as_view(), name='detail'), + url(r'^(?P\d+)/results/$', views.ResultsView.as_view(), name='results'), + url(r'^(?P\d+)/vote/$', views.vote, name='vote'), +) \ No newline at end of file diff --git a/mysite/polls/views.py b/mysite/polls/views.py new file mode 100644 index 0000000..82a11b3 --- /dev/null +++ b/mysite/polls/views.py @@ -0,0 +1,44 @@ +from django.shortcuts import get_object_or_404, render +from django.http import HttpResponseRedirect +from django.core.urlresolvers import reverse +from django.views import generic + +from models import Choice, Question + + +class IndexView(generic.ListView): + template_name = 'polls/index.html' + context_object_name = 'latest_question_list' + + def get_queryset(self): + """Return the last five published questions.""" + return Question.objects.order_by('-pub_date')[:5] + + +class DetailView(generic.DetailView): + model = Question + template_name = 'polls/detail.html' + + +class ResultsView(generic.DetailView): + model = Question + template_name = 'polls/results.html' + + +def vote(request, question_id): + p = get_object_or_404(Question, pk=question_id) + try: + selected_choice = p.choice_set.get(pk=request.POST['choice']) + except (KeyError, Choice.DoesNotExist): + # Redisplay the question voting form. + return render(request, 'polls/detail.html', { + 'question': p, + 'error_message': "You didn't select a choice.", + }) + 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('polls:results', args=(p.id,))) \ No newline at end of file diff --git a/mysite/templates/admin/base_site.html b/mysite/templates/admin/base_site.html new file mode 100644 index 0000000..7e9c91f --- /dev/null +++ b/mysite/templates/admin/base_site.html @@ -0,0 +1,9 @@ +{% extends "admin/base.html" %} + +{% block title %}{{ title }} | {{ site_title|default:_('Django site admin') }}{% endblock %} + +{% block branding %} +

{{ 'Task tracker'|default:_('Django administration') }}

+{% endblock %} + +{% block nav-global %}{% endblock %} \ No newline at end of file diff --git a/mysite/templates/admin/index.html b/mysite/templates/admin/index.html new file mode 100644 index 0000000..fcf269e --- /dev/null +++ b/mysite/templates/admin/index.html @@ -0,0 +1,82 @@ +{% extends "admin/base_site.html" %} +{% load i18n admin_static %} + +{% block extrastyle %}{{ block.super }}{% endblock %} + +{% block coltype %}colMS{% endblock %} + +{% block bodyclass %}{{ block.super }} dashboard{% endblock %} + +{% block breadcrumbs %}{% endblock %} + +{% block content %} +
+ +{% if app_list %} + {% for app in app_list %} +
+ + + {% for model in app.models %} + + {% if model.admin_url %} + + {% else %} + + {% endif %} + + {% if model.add_url %} + + {% else %} + + {% endif %} + + {% if model.admin_url %} + + {% else %} + + {% endif %} + + {% endfor %} +
+ {{ app.name }} +
{{ model.name }}{{ model.name }}{% trans 'Add' %} {% trans 'Change' %} 
+
+ {% endfor %} +{% else %} +

{% trans "You don't have permission to edit anything." %}

+{% endif %} +
+{% endblock %} + +{% block sidebar %} + +{% endblock %} diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..2f2e6fa --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +Django==1.7.1 \ No newline at end of file