From 81f35fe6b615ec6202e797d08b1bd7dda5b984c9 Mon Sep 17 00:00:00 2001 From: gremlinLee <90wertigo> Date: Sun, 30 Nov 2014 22:39:06 +0200 Subject: [PATCH] My task. Login: admin, password: admin. I have done four steps of django tutorial. --- mysite/db.sqlite3 | Bin 0 -> 40960 bytes mysite/manage.py | 10 +++ mysite/mysite/__init__.py | 0 mysite/mysite/settings.py | 86 ++++++++++++++++++++++ mysite/mysite/urls.py | 7 ++ mysite/mysite/wsgi.py | 14 ++++ mysite/polls/__init__.py | 0 mysite/polls/admin.py | 21 ++++++ mysite/polls/migrations/0001_initial.py | 41 +++++++++++ mysite/polls/migrations/__init__.py | 0 mysite/polls/models.py | 28 +++++++ mysite/polls/templates/polls/detail.html | 12 +++ mysite/polls/templates/polls/index.html | 9 +++ mysite/polls/templates/polls/results.html | 9 +++ mysite/polls/tests.py | 3 + mysite/polls/urls.py | 10 +++ mysite/polls/views.py | 44 +++++++++++ mysite/templates/admin/base_site.html | 9 +++ mysite/templates/admin/index.html | 82 +++++++++++++++++++++ requirements.txt | 1 + 20 files changed, 386 insertions(+) create mode 100644 mysite/db.sqlite3 create mode 100755 mysite/manage.py create mode 100644 mysite/mysite/__init__.py create mode 100644 mysite/mysite/settings.py create mode 100644 mysite/mysite/urls.py create mode 100644 mysite/mysite/wsgi.py create mode 100644 mysite/polls/__init__.py create mode 100644 mysite/polls/admin.py create mode 100644 mysite/polls/migrations/0001_initial.py create mode 100644 mysite/polls/migrations/__init__.py create mode 100644 mysite/polls/models.py create mode 100644 mysite/polls/templates/polls/detail.html create mode 100644 mysite/polls/templates/polls/index.html create mode 100644 mysite/polls/templates/polls/results.html create mode 100644 mysite/polls/tests.py create mode 100644 mysite/polls/urls.py create mode 100644 mysite/polls/views.py create mode 100644 mysite/templates/admin/base_site.html create mode 100644 mysite/templates/admin/index.html create mode 100644 requirements.txt diff --git a/mysite/db.sqlite3 b/mysite/db.sqlite3 new file mode 100644 index 0000000000000000000000000000000000000000..0e4c1a258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#|S{~e{)R<-rHQfK3?7>=R7WN?Pgq4BLqj}uq4N?;D6-4z;HYqi8Q01 zVeh&}-51Ge|y4=a|=5cp&waJ8{m z&!Yrvy!>-y{0r!Pj1E@NbcqscHJYrr?^S2EAzyzWux)zTh|Rqj4M_ zWz-11yytSTHhNHpK)Xt)HZ5gjv7f)$4$ACDTlf+x@Zw?mUui%Pqdr*m(vwVkQ>3Db z5?H>hs`f}HOuM;Ccj_CWENWux13wUt?{`m!t^B^4XcxG4Cd=rNY SZ1UVvOHB~_Jf2(G{Qm(Tl{rrU literal 0 HcmV?d00001 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 }}

+ +
    + {% for choice in question.choice_set.all %} +
  • {{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}
  • + {% endfor %} +
+ +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