Skip to content

Commit 1da4a3c

Browse files
committed
update stage 3 blog for py3/dj1.9
1 parent 2d5722e commit 1da4a3c

File tree

12 files changed

+135
-64
lines changed

12 files changed

+135
-64
lines changed
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from django.contrib import admin
2-
from myblog.models import Post, Category
2+
3+
from myblog.models import Category
4+
from myblog.models import Post
35

46

5-
admin.site.register(Post)
67
admin.site.register(Category)
8+
admin.site.register(Post)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.apps import AppConfig
2+
3+
4+
class MyblogConfig(AppConfig):
5+
name = 'myblog'
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
# -*- coding: utf-8 -*-
2+
# Generated by Django 1.9 on 2015-12-31 19:13
23
from __future__ import unicode_literals
34

4-
from django.db import models, migrations
55
from django.conf import settings
6+
from django.db import migrations, models
7+
import django.db.models.deletion
68

79

810
class Migration(migrations.Migration):
911

12+
initial = True
13+
1014
dependencies = [
1115
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
1216
]
@@ -15,16 +19,13 @@ class Migration(migrations.Migration):
1519
migrations.CreateModel(
1620
name='Post',
1721
fields=[
18-
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
22+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
1923
('title', models.CharField(max_length=128)),
2024
('text', models.TextField(blank=True)),
2125
('created_date', models.DateTimeField(auto_now_add=True)),
2226
('modified_date', models.DateTimeField(auto_now=True)),
23-
('published_date', models.DateTimeField(null=True, blank=True)),
24-
('author', models.ForeignKey(to=settings.AUTH_USER_MODEL)),
27+
('published_date', models.DateTimeField(blank=True, null=True)),
28+
('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
2529
],
26-
options={
27-
},
28-
bases=(models.Model,),
2930
),
3031
]
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# -*- coding: utf-8 -*-
2+
# Generated by Django 1.9 on 2015-12-31 21:40
23
from __future__ import unicode_literals
34

4-
from django.db import models, migrations
5+
from django.db import migrations, models
56

67

78
class Migration(migrations.Migration):
@@ -14,13 +15,10 @@ class Migration(migrations.Migration):
1415
migrations.CreateModel(
1516
name='Category',
1617
fields=[
17-
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
18+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
1819
('name', models.CharField(max_length=128)),
1920
('description', models.TextField(blank=True)),
20-
('posts', models.ManyToManyField(related_name='categories', null=True, to='myblog.Post', blank=True)),
21+
('posts', models.ManyToManyField(blank=True, related_name='categories', to='myblog.Post')),
2122
],
22-
options={
23-
},
24-
bases=(models.Model,),
2523
),
2624
]

resources/session08/mysite_stage_3/myblog/models.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,18 @@ class Post(models.Model):
1010
modified_date = models.DateTimeField(auto_now=True)
1111
published_date = models.DateTimeField(blank=True, null=True)
1212

13-
def __unicode__(self):
13+
def __str__(self):
1414
return self.title
1515

1616

1717
class Category(models.Model):
1818
name = models.CharField(max_length=128)
1919
description = models.TextField(blank=True)
20-
posts = models.ManyToManyField(Post, blank=True, null=True,
21-
related_name='categories')
20+
posts = models.ManyToManyField(
21+
Post,
22+
blank=True,
23+
related_name='categories'
24+
)
2225

23-
def __unicode__(self):
26+
def __str__(self):
2427
return self.name

resources/session08/mysite_stage_3/myblog/tests.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
11
import datetime
2-
from django.test import TestCase
32
from django.contrib.auth.models import User
3+
from django.test import TestCase
44
from django.utils.timezone import utc
5-
from myblog.models import Post, Category
5+
6+
from myblog.models import Category
7+
from myblog.models import Post
68

79

810
class PostTestCase(TestCase):
9-
fixtures = ['myblog_test_fixture.json', ]
11+
fixtures = ['myblog_test_fixture.json']
1012

1113
def setUp(self):
1214
self.user = User.objects.get(pk=1)
1315

14-
def test_unicode(self):
15-
expected = u"This is a title"
16+
def test_string_representation(self):
17+
expected = "This is a title"
1618
p1 = Post(title=expected)
17-
actual = unicode(p1)
19+
actual = str(p1)
1820
self.assertEqual(expected, actual)
1921

2022

2123
class CategoryTestCase(TestCase):
2224

23-
def test_unicode(self):
25+
def test_string_representation(self):
2426
expected = "A Category"
2527
c1 = Category(name=expected)
26-
actual = unicode(c1)
28+
actual = str(c1)
2729
self.assertEqual(expected, actual)
2830

2931

@@ -47,7 +49,9 @@ def setUp(self):
4749

4850
def test_list_only_published(self):
4951
resp = self.client.get('/')
50-
self.assertTrue("Recent Posts" in resp.content)
52+
# the content of the rendered response is always a bytestring
53+
resp_text = resp.content.decode(resp.charset)
54+
self.assertTrue("Recent Posts" in resp_text)
5155
for count in range(1, 11):
5256
title = "Post %d Title" % count
5357
if count < 6:
Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
from django.conf.urls import patterns, url
1+
from django.conf.urls import url
22

3+
from myblog.views import stub_view
4+
from myblog.views import list_view
5+
from myblog.views import detail_view
36

4-
urlpatterns = patterns(
5-
'myblog.views',
7+
8+
urlpatterns = [
69
url(r'^$',
7-
'list_view',
10+
list_view,
811
name="blog_index"),
912
url(r'^posts/(?P<post_id>\d+)/$',
10-
'detail_view',
11-
name="blog_detail"),
12-
)
13+
detail_view,
14+
name='blog_detail'),
15+
]

resources/session08/mysite_stage_3/myblog/views.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
from django.shortcuts import render
21
from django.http import HttpResponse, HttpResponseRedirect, Http404
2+
from django.shortcuts import render
33
from django.template import RequestContext, loader
4+
45
from myblog.models import Post
56

67

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,79 @@
11
"""
22
Django settings for mysite project.
33
4+
Generated by 'django-admin startproject' using Django 1.9.
5+
46
For more information on this file, see
5-
https://docs.djangoproject.com/en/1.7/topics/settings/
7+
https://docs.djangoproject.com/en/1.9/topics/settings/
68
79
For the full list of settings and their values, see
8-
https://docs.djangoproject.com/en/1.7/ref/settings/
10+
https://docs.djangoproject.com/en/1.9/ref/settings/
911
"""
1012

11-
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
1213
import os
13-
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
14+
15+
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
16+
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
1417

1518

1619
# Quick-start development settings - unsuitable for production
17-
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/
20+
# See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/
1821

1922
# SECURITY WARNING: keep the secret key used in production secret!
20-
SECRET_KEY = 'e@3=0i!#n4l25r*ul*sbx6b$@gh7a6pjee6lr-slw9!ayj#*@f'
23+
SECRET_KEY = 'i=n^tc%@@gq#8ev6dlymy9+-%@^f!q54sjf0rvikt_k5bl(t1='
2124

2225
# SECURITY WARNING: don't run with debug turned on in production!
2326
DEBUG = True
2427

25-
TEMPLATE_DEBUG = True
26-
2728
ALLOWED_HOSTS = []
2829

2930

3031
# Application definition
3132

32-
INSTALLED_APPS = (
33+
INSTALLED_APPS = [
3334
'django.contrib.admin',
3435
'django.contrib.auth',
3536
'django.contrib.contenttypes',
3637
'django.contrib.sessions',
3738
'django.contrib.messages',
3839
'django.contrib.staticfiles',
3940
'myblog',
40-
)
41+
]
4142

42-
MIDDLEWARE_CLASSES = (
43+
MIDDLEWARE_CLASSES = [
44+
'django.middleware.security.SecurityMiddleware',
4345
'django.contrib.sessions.middleware.SessionMiddleware',
4446
'django.middleware.common.CommonMiddleware',
4547
'django.middleware.csrf.CsrfViewMiddleware',
4648
'django.contrib.auth.middleware.AuthenticationMiddleware',
4749
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
4850
'django.contrib.messages.middleware.MessageMiddleware',
4951
'django.middleware.clickjacking.XFrameOptionsMiddleware',
50-
)
52+
]
5153

5254
ROOT_URLCONF = 'mysite.urls'
5355

56+
TEMPLATES = [
57+
{
58+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
59+
'DIRS': [os.path.join(BASE_DIR, 'mysite/templates')],
60+
'APP_DIRS': True,
61+
'OPTIONS': {
62+
'context_processors': [
63+
'django.template.context_processors.debug',
64+
'django.template.context_processors.request',
65+
'django.contrib.auth.context_processors.auth',
66+
'django.contrib.messages.context_processors.messages',
67+
],
68+
},
69+
},
70+
]
71+
5472
WSGI_APPLICATION = 'mysite.wsgi.application'
5573

5674

5775
# Database
58-
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases
76+
# https://docs.djangoproject.com/en/1.9/ref/settings/#databases
5977

6078
DATABASES = {
6179
'default': {
@@ -64,8 +82,28 @@
6482
}
6583
}
6684

85+
86+
# Password validation
87+
# https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators
88+
89+
AUTH_PASSWORD_VALIDATORS = [
90+
{
91+
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
92+
},
93+
{
94+
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
95+
},
96+
{
97+
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
98+
},
99+
{
100+
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
101+
},
102+
]
103+
104+
67105
# Internationalization
68-
# https://docs.djangoproject.com/en/1.7/topics/i18n/
106+
# https://docs.djangoproject.com/en/1.9/topics/i18n/
69107

70108
LANGUAGE_CODE = 'en-us'
71109

@@ -79,11 +117,9 @@
79117

80118

81119
# Static files (CSS, JavaScript, Images)
82-
# https://docs.djangoproject.com/en/1.7/howto/static-files/
120+
# https://docs.djangoproject.com/en/1.9/howto/static-files/
83121

84122
STATIC_URL = '/static/'
85123

86-
87-
TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'mysite/templates'), )
88124
LOGIN_URL = '/login/'
89125
LOGIN_REDIRECT_URL = '/'

resources/session08/mysite_stage_3/mysite/templates/base.html

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
<div id="header">
1010
<ul id="control-bar">
1111
{% if user.is_authenticated %}
12-
{% if user.is_staff %}
13-
<li><a href="{% url 'admin:index' %}">admin</a></li>
14-
{% endif %}
12+
{% if user.is_staff %}<li><a href="{% url 'admin:index' %}">admin</a></li>{% endif %}
1513
<li><a href="{% url 'logout' %}">logout</a></li>
1614
{% else %}
1715
<li><a href="{% url 'login' %}">login</a></li>

0 commit comments

Comments
 (0)