From 1df43209ca992425029323c65e9ef61014632ee6 Mon Sep 17 00:00:00 2001 From: mmanc125uw Date: Tue, 29 Sep 2020 06:31:21 -0400 Subject: [PATCH 1/8] sp_py230 lesson7 activity, initial steps --- blogging/admin.py | 2 +- blogging/migrations/0001_initial.py | 2 +- blogging/migrations/0002_category.py | 2 +- blogging/models.py | 11 ++-- blogging/tests.py | 63 +------------------- blogging/urls.py | 6 +- blogging/views.py | 31 +++++----- goDjangoSrvStart.bat | 2 + mysite/settings.py | 7 +-- mysite/templates/base.html | 12 ---- mysite/urls.py | 23 +------ polling/admin.py | 1 + polling/migrations/0001_initial.py | 2 +- polling/models.py | 1 + polling/templates/polling/list.html | 2 + polling/urls.py | 2 + readmeSetupSteps.txt | 89 ++++++++++++++++++++++++++++ requirements.txt | 2 +- 18 files changed, 135 insertions(+), 125 deletions(-) create mode 100644 goDjangoSrvStart.bat create mode 100644 readmeSetupSteps.txt diff --git a/blogging/admin.py b/blogging/admin.py index 1175916..10b3083 100644 --- a/blogging/admin.py +++ b/blogging/admin.py @@ -1,6 +1,6 @@ +# blogging/admin.py from django.contrib import admin from blogging.models import Post, Category - admin.site.register(Post) admin.site.register(Category) diff --git a/blogging/migrations/0001_initial.py b/blogging/migrations/0001_initial.py index 5d406bf..cfee129 100644 --- a/blogging/migrations/0001_initial.py +++ b/blogging/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 2.1.1 on 2019-10-29 01:39 +# Generated by Django 2.1.5 on 2020-09-23 19:38 from django.conf import settings from django.db import migrations, models diff --git a/blogging/migrations/0002_category.py b/blogging/migrations/0002_category.py index 0ccbe19..e7ed339 100644 --- a/blogging/migrations/0002_category.py +++ b/blogging/migrations/0002_category.py @@ -1,4 +1,4 @@ -# Generated by Django 2.1.1 on 2019-11-05 03:35 +# Generated by Django 2.1.5 on 2020-09-28 20:10 from django.db import migrations, models diff --git a/blogging/models.py b/blogging/models.py index 10d6cc3..ed3a85e 100644 --- a/blogging/models.py +++ b/blogging/models.py @@ -1,4 +1,4 @@ -from django.db import models +from django.db import models # <-- This is already in the file from django.contrib.auth.models import User class Post(models.Model): @@ -12,13 +12,16 @@ class Post(models.Model): def __str__(self): return self.title + class Category(models.Model): + pass name = models.CharField(max_length=128) description = models.TextField(blank=True) posts = models.ManyToManyField(Post, blank=True, related_name='categories') + def __str__(self): + return self.name + class Meta: - verbose_name_plural = 'Categories' + verbose_name_plural = 'Categories' - def __str__(self): - return self.name diff --git a/blogging/tests.py b/blogging/tests.py index 4250226..f10df63 100644 --- a/blogging/tests.py +++ b/blogging/tests.py @@ -1,72 +1,11 @@ -import datetime - from django.test import TestCase -from django.contrib.auth.models import User -from django.utils.timezone import utc - -from blogging.models import Post from blogging.models import Category -class PostTestCase(TestCase): - fixtures = ['blogging_test_fixture.json', ] - - def setUp(self): - self.user = User.objects.get(pk=1) - - def test_string_representation(self): - expected = "This is a title" - p1 = Post(title=expected) - actual = str(p1) - self.assertEqual(expected, actual) - - class CategoryTestCase(TestCase): def test_string_representation(self): expected = "A Category" c1 = Category(name=expected) actual = str(c1) - self.assertEqual(expected, actual) - - -class FrontEndTestCase(TestCase): - """test views provided in the front-end""" - fixtures = ['blogging_test_fixture.json', ] - - def setUp(self): - self.now = datetime.datetime.utcnow().replace(tzinfo=utc) - self.timedelta = datetime.timedelta(15) - author = User.objects.get(pk=1) - for count in range(1, 11): - post = Post(title="Post %d Title" % count, - text="foo", - author=author) - if count < 6: - # publish the first five posts - pubdate = self.now - self.timedelta * count - post.published_date = pubdate - post.save() - - def test_list_only_published(self): - resp = self.client.get('/') - # the content of the rendered response is always a bytestring - resp_text = resp.content.decode(resp.charset) - self.assertTrue("Recent Posts" in resp_text) - for count in range(1, 11): - title = "Post %d Title" % count - if count < 6: - self.assertContains(resp, title, count=1) - else: - self.assertNotContains(resp, title) - - def test_details_only_published(self): - for count in range(1, 11): - title = "Post %d Title" % count - post = Post.objects.get(title=title) - resp = self.client.get('/posts/%d/' % post.pk) - if count < 6: - self.assertEqual(resp.status_code, 200) - self.assertContains(resp, title) - else: - self.assertEqual(resp.status_code, 404) + self.assertEqual(expected, actual) \ No newline at end of file diff --git a/blogging/urls.py b/blogging/urls.py index 172a39c..dce7aa0 100644 --- a/blogging/urls.py +++ b/blogging/urls.py @@ -1,7 +1,9 @@ +# polling/urls.py + from django.urls import path from blogging.views import list_view, detail_view urlpatterns = [ - path('', list_view, name="blog_index"), - path('posts//', detail_view, name="blog_detail"), + path('', list_view, name="poll_index"), + path('polls//', detail_view, name="poll_detail"), ] diff --git a/blogging/views.py b/blogging/views.py index b4bab4f..6339808 100644 --- a/blogging/views.py +++ b/blogging/views.py @@ -1,22 +1,23 @@ from django.shortcuts import render -from django.http import HttpResponse, HttpResponseRedirect, Http404 -from django.template import loader - -from blogging.models import Post +from django.http import Http404 +from polling.models import Poll +def list_view(request): + context = {'polls': Poll.objects.all()} + return render(request, 'polling/list.html', context) -def detail_view(request, post_id): - published = Post.objects.exclude(published_date__exact=None) +def detail_view(request, poll_id): try: - post = published.get(pk=post_id) - except Post.DoesNotExist: + poll = Poll.objects.get(pk=poll_id) + except Poll.DoesNotExist: raise Http404 - context = {'post': post} - return render(request, 'blogging/detail.html', context) + if request.method == "POST": + if request.POST.get("vote") == "Yes": + poll.score += 1 + else: + poll.score -= 1 + poll.save() -def list_view(request): - published = Post.objects.exclude(published_date__exact=None) - posts = published.order_by('-published_date') - context = {'posts': posts} - return render(request, 'blogging/list.html', context) + context = {'poll': poll} + return render(request, 'polling/detail.html', context) diff --git a/goDjangoSrvStart.bat b/goDjangoSrvStart.bat new file mode 100644 index 0000000..c445e85 --- /dev/null +++ b/goDjangoSrvStart.bat @@ -0,0 +1,2 @@ + +start python manage.py runserver diff --git a/mysite/settings.py b/mysite/settings.py index 14e4a11..65a7bdc 100644 --- a/mysite/settings.py +++ b/mysite/settings.py @@ -20,7 +20,7 @@ # See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = 'xak=ca*e6hvh8q5hgfz5l9ees)_pxjif0)ui!ikifg4!enjk+7' +SECRET_KEY = 'zn5wbhnx97(^r(_l-8hg^s)kw4hr@f6fz=4+l2or423&+zgw7r' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True @@ -56,7 +56,7 @@ TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'DIRS': [os.path.join(BASE_DIR, 'mysite/templates')], + 'DIRS': [os.path.join(BASE_DIR, 'mysite/templates')], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ @@ -120,6 +120,3 @@ # https://docs.djangoproject.com/en/2.1/howto/static-files/ STATIC_URL = '/static/' - -LOGIN_URL = '/login/' -LOGIN_REDIRECT_URL = '/' diff --git a/mysite/templates/base.html b/mysite/templates/base.html index 4bb0230..2eacafa 100644 --- a/mysite/templates/base.html +++ b/mysite/templates/base.html @@ -1,21 +1,9 @@ -{% load staticfiles %} My Django Blog - - {# header ends here #}
{% block content %} diff --git a/mysite/urls.py b/mysite/urls.py index 446cd8f..903e7bd 100644 --- a/mysite/urls.py +++ b/mysite/urls.py @@ -1,27 +1,10 @@ -"""mysite URL Configuration -The `urlpatterns` list routes URLs to views. For more information please see: - https://docs.djangoproject.com/en/2.1/topics/http/urls/ -Examples: -Function views - 1. Add an import: from my_app import views - 2. Add a URL to urlpatterns: path('', views.home, name='home') -Class-based views - 1. Add an import: from other_app.views import Home - 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') -Including another URLconf - 1. Import the include() function: from django.urls import include, path - 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) -""" from django.contrib import admin -from django.urls import path, include -from django.contrib.auth.views import LoginView, LogoutView +from django.urls import path, include # <-- Make sure you have both of these imports. urlpatterns = [ - path('', include('blogging.urls')), - path('polling/', include('polling.urls')), + path('polling/', include('polling.urls')), # <-- Add this + path('blogging/', include('blogging.urls')), # <-- Add this path('admin/', admin.site.urls), - path('login/', LoginView.as_view(template_name='login.html'), name="login"), - path('logout/', LogoutView.as_view(next_page='/'), name="logout"), ] diff --git a/polling/admin.py b/polling/admin.py index 15a1f46..1874dd8 100644 --- a/polling/admin.py +++ b/polling/admin.py @@ -1,3 +1,4 @@ +# blogging/admin.py from django.contrib import admin from polling.models import Poll diff --git a/polling/migrations/0001_initial.py b/polling/migrations/0001_initial.py index 2be60e7..2f44dbc 100644 --- a/polling/migrations/0001_initial.py +++ b/polling/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 2.1.1 on 2019-10-29 01:24 +# Generated by Django 2.1.1 on 2020-09-23 14:25 from django.db import migrations, models diff --git a/polling/models.py b/polling/models.py index 6a940d2..fa8da99 100644 --- a/polling/models.py +++ b/polling/models.py @@ -1,3 +1,4 @@ +# blogging/models.py from django.db import models class Poll(models.Model): diff --git a/polling/templates/polling/list.html b/polling/templates/polling/list.html index 9cf4282..fc36de4 100644 --- a/polling/templates/polling/list.html +++ b/polling/templates/polling/list.html @@ -1,3 +1,5 @@ +{# polling/templates/polling/list.html #} + {% extends "base.html" %} {% block content %}

Polls

diff --git a/polling/urls.py b/polling/urls.py index 9c4e2fd..e3560bc 100644 --- a/polling/urls.py +++ b/polling/urls.py @@ -1,3 +1,5 @@ +# polling/urls.py + from django.urls import path from polling.views import list_view, detail_view diff --git a/readmeSetupSteps.txt b/readmeSetupSteps.txt new file mode 100644 index 0000000..4e2d6d6 --- /dev/null +++ b/readmeSetupSteps.txt @@ -0,0 +1,89 @@ + +rmvirtualenv djangoenv ;***MMM only if already exists +mkvirtualenv djangoenv +pip install django==2.1.5 ;***MMM DO NOT USE 2.1.1 THROWS EXCEPTIONS + +django-admin help +django-admin startproject mysite +cd mysite +python manage.py runserver ; start django +open a browser url = localhost:8000 ; test +python manage.py migrate ; create the database (see section in mysite\settings.py) +dir ; note the db.sqlite3 db is created +winpty python manage.py createsuperuser ; create a superuser to admin the db + user = mike + password = Password1! +python manage.py startapp polling ; create a polling app +dir ; note the polling created app folder +edit mysite\settings + section: INSTALL_APPS + add your polling app to the bottom of the list + 'polling', ; ***MMM be sure to include the single quotes +git init + +create the overall site web templates +mkdir mysite\templates +edit mysite\templates\base.html ; ref the video for the code +edit mysite\settings.py ; add 'DIRS': [os.path.join(BASE_DIR, 'mysite/templates')], + +edit mysite\polling\models.py ; ref the video +python manage.py makemigrations ; migrate the changes to the db +python manage.py migrate +edit mysite\polling\admin.py ; register the poll model +start python manage.py runserver ; # Then visit http://localhost:8000/admin/ +mkdir mysite\polling\templates\polling ; we create polling view templates under our polling app +edit mysite\polling\templates\polling\list.html ; create template, ref the video +edit mysite\polling\templates\polling\detail.html ; create template, ref the video +edit mysite\polling\views.py ; add the view for the template, ref the video +edit mysite\urls.py ; define the route to your top level site in urlpatterns, see the video +edit mysite\polling\urls.py ; routes for the polling app +start python manage.py runserver ; # Then visit http://localhost:8000/polling/ + +python manage.py startapp blogging ; create the blogging app +edit mysite\settings + section: INSTALL_APPS + add your polling app to the bottom of the list + 'blogging', ; ***MMM be sure to include the single quotes +python manage.py makemigrations blogging ; migrate the table changes to the db for blogging +python manage.py migrate ; update the db + +- using the django shell to test interactively +python manage.py shell +from blogging.models import Post +from django.contrib.auth.models import User +all_users = User.objects.all() +p2 = Post(title="Another post", + text="The second one created", + author=all_users[0]).save() +p3 = Post(title="The third one", + text="With the word 'heffalump'", + author=all_users[0]).save() +p4 = Post(title="Posters are a great decoration", + text="When you are a poor college student", + author=all_users[0]).save() +Post.objects.count() + +- the blogging app via admin +edit mysite\blogging\admin.py ; ref the video +python manage.py runserver ; start django + +------------------ +lesson 7 activity + +edit blogging/models.py ; per lesson, ie add Category class +python manage.py makemigrations ; comit to the model using migrations +python manage.py migrate ; comit to the db +edit blogging/admin.py ; add Category +python manage.py runserver +http://localhost:8000/admin ; test categories appears in admin +edit tests.py ; test give a name to the displayed category +python manage.py test blogging ; one test will fail +edit blogging/models.py ; per lesson +retest ; verify categories, and name appears + + + + + + + diff --git a/requirements.txt b/requirements.txt index 81580ab..6214584 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,2 @@ -Django==2.1.1 +Django==2.1.5 pytz==2019.3 From d97a33924de51b4be5988cb177d11c7c67f6aae5 Mon Sep 17 00:00:00 2001 From: mmanc125uw Date: Tue, 29 Sep 2020 09:55:48 -0400 Subject: [PATCH 2/8] sp_py230 lesson7 activity, begin list view --- blogging/tests.py | 53 +++++++++++++++++++++++++++++++++++++++++++++-- blogging/urls.py | 11 +++++----- blogging/views.py | 35 ++++++++++++------------------- 3 files changed, 70 insertions(+), 29 deletions(-) diff --git a/blogging/tests.py b/blogging/tests.py index f10df63..e06df93 100644 --- a/blogging/tests.py +++ b/blogging/tests.py @@ -1,6 +1,11 @@ +import datetime + from django.test import TestCase -from blogging.models import Category +from django.contrib.auth.models import User +from django.utils.timezone import utc +from blogging.models import Post +from blogging.models import Category class CategoryTestCase(TestCase): @@ -8,4 +13,48 @@ def test_string_representation(self): expected = "A Category" c1 = Category(name=expected) actual = str(c1) - self.assertEqual(expected, actual) \ No newline at end of file + self.assertEqual(expected, actual) + + +class FrontEndTestCase(TestCase): + """test views provided in the front-end""" + fixtures = ['blogging_test_fixture.json', ] + + def setUp(self): + self.now = datetime.datetime.utcnow().replace(tzinfo=utc) + self.timedelta = datetime.timedelta(15) + author = User.objects.get(pk=1) + for count in range(1, 11): + post = Post(title="Post %d Title" % count, + text="foo", + author=author) + if count < 6: + # publish the first five posts + pubdate = self.now - self.timedelta * count + post.published_date = pubdate + post.save() + + def test_list_only_published(self): + resp = self.client.get('/') + # the content of the rendered response is always a bytestring + resp_text = resp.content.decode(resp.charset) + self.assertTrue("Recent Posts" in resp_text) + for count in range(1, 11): + title = "Post %d Title" % count + if count < 6: + self.assertContains(resp, title, count=1) + else: + self.assertNotContains(resp, title) + + """ + def test_details_only_published(self): + for count in range(1, 11): + title = "Post %d Title" % count + post = Post.objects.get(title=title) + resp = self.client.get('/posts/%d/' % post.pk) + if count < 6: + self.assertEqual(resp.status_code, 200) + self.assertContains(resp, title) + else: + self.assertEqual(resp.status_code, 404) + """ diff --git a/blogging/urls.py b/blogging/urls.py index dce7aa0..703bb98 100644 --- a/blogging/urls.py +++ b/blogging/urls.py @@ -1,9 +1,10 @@ -# polling/urls.py - from django.urls import path -from blogging.views import list_view, detail_view + +from blogging.views import stub_view urlpatterns = [ - path('', list_view, name="poll_index"), - path('polls//', detail_view, name="poll_detail"), + path('', stub_view, name="blog_index"), + path('posts//', stub_view, name="blog_detail"), ] + + diff --git a/blogging/views.py b/blogging/views.py index 6339808..68aad67 100644 --- a/blogging/views.py +++ b/blogging/views.py @@ -1,23 +1,14 @@ from django.shortcuts import render -from django.http import Http404 -from polling.models import Poll - -def list_view(request): - context = {'polls': Poll.objects.all()} - return render(request, 'polling/list.html', context) - -def detail_view(request, poll_id): - try: - poll = Poll.objects.get(pk=poll_id) - except Poll.DoesNotExist: - raise Http404 - - if request.method == "POST": - if request.POST.get("vote") == "Yes": - poll.score += 1 - else: - poll.score -= 1 - poll.save() - - context = {'poll': poll} - return render(request, 'polling/detail.html', context) +from blogging.models import Post, Category +from django.http import HttpResponse, HttpResponseRedirect, Http404 + + +def stub_view(request, *args, **kwargs): + body = "Stub View\n\n" + if args: + body += "Args:\n" + body += "\n".join(["\t%s" % a for a in args]) + if kwargs: + body += "Kwargs:\n" + body += "\n".join(["\t%s: %s" % i for i in kwargs.items()]) + return HttpResponse(body, content_type="text/plain") From e81934c58f0545ae4e43d619bafabbb2da529510 Mon Sep 17 00:00:00 2001 From: mmanc125uw Date: Tue, 29 Sep 2020 10:03:22 -0400 Subject: [PATCH 3/8] sp_py230 lesson7 activity, updated urls.py for home --- mysite/urls.py | 1 + 1 file changed, 1 insertion(+) diff --git a/mysite/urls.py b/mysite/urls.py index 903e7bd..d5b24b3 100644 --- a/mysite/urls.py +++ b/mysite/urls.py @@ -4,6 +4,7 @@ urlpatterns = [ + path('', include('blogging.urls')), # <-- add this path('polling/', include('polling.urls')), # <-- Add this path('blogging/', include('blogging.urls')), # <-- Add this path('admin/', admin.site.urls), From 2c0928f71d3a7b4e527c6b62da446e82ca09c795 Mon Sep 17 00:00:00 2001 From: mmanc125uw Date: Tue, 29 Sep 2020 14:19:25 -0400 Subject: [PATCH 4/8] sp_py230 lesson7 activity, add list_view --- blogging/urls.py | 5 +++-- blogging/views.py | 9 +++++++++ readmeSetupSteps.txt | 8 ++++++-- test_results_blogging.txt | 8 ++++++++ 4 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 test_results_blogging.txt diff --git a/blogging/urls.py b/blogging/urls.py index 703bb98..8ca5acf 100644 --- a/blogging/urls.py +++ b/blogging/urls.py @@ -1,9 +1,10 @@ from django.urls import path -from blogging.views import stub_view +from blogging.views import stub_view, list_view urlpatterns = [ - path('', stub_view, name="blog_index"), +### path('', stub_view, name="blog_index"), + path('', list_view, name="blog_index"), path('posts//', stub_view, name="blog_detail"), ] diff --git a/blogging/views.py b/blogging/views.py index 68aad67..99801a9 100644 --- a/blogging/views.py +++ b/blogging/views.py @@ -1,6 +1,7 @@ from django.shortcuts import render from blogging.models import Post, Category from django.http import HttpResponse, HttpResponseRedirect, Http404 +from django.template import loader def stub_view(request, *args, **kwargs): @@ -12,3 +13,11 @@ def stub_view(request, *args, **kwargs): body += "Kwargs:\n" body += "\n".join(["\t%s: %s" % i for i in kwargs.items()]) return HttpResponse(body, content_type="text/plain") + +def list_view(request): + published = Post.objects.exclude(published_date__exact=None) + posts = published.order_by('-published_date') + template = loader.get_template('blogging/list.html') + context = {'posts': posts} + body = template.render(context) + return HttpResponse(body, content_type="text/html") diff --git a/readmeSetupSteps.txt b/readmeSetupSteps.txt index 4e2d6d6..17c270d 100644 --- a/readmeSetupSteps.txt +++ b/readmeSetupSteps.txt @@ -80,8 +80,12 @@ edit tests.py ; test give a name to the displayed category python manage.py test blogging ; one test will fail edit blogging/models.py ; per lesson retest ; verify categories, and name appears - - +edit blogging/views.py ; per lesson +edit blogging/url.conf ; per lesson +add this to misite/misite/urls.py ; path('', include('blogging.urls')), +edit blogging/tests.py ; per lesson add test_list_only_published +edit blogging/templates/blogging/list.html ; per lesson, list html +edit blogging/view.html ; per lesson, add list view diff --git a/test_results_blogging.txt b/test_results_blogging.txt new file mode 100644 index 0000000..8d4a461 --- /dev/null +++ b/test_results_blogging.txt @@ -0,0 +1,8 @@ +.. +---------------------------------------------------------------------- +Ran 2 tests in 0.090s + +OK +Creating test database for alias 'default'... +System check identified no issues (0 silenced). +Destroying test database for alias 'default'... From cefe3e287a5e5825a9a17f20fd180f621fda0021 Mon Sep 17 00:00:00 2001 From: mmanc125uw Date: Tue, 29 Sep 2020 14:28:01 -0400 Subject: [PATCH 5/8] sp_py230 test rerun --- blogging/tests.py | 13 +++++++++++++ test_results_blogging.txt | 5 ----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/blogging/tests.py b/blogging/tests.py index e06df93..adbef52 100644 --- a/blogging/tests.py +++ b/blogging/tests.py @@ -7,6 +7,19 @@ from blogging.models import Post from blogging.models import Category + +class PostTestCase(TestCase): + fixtures = ['blogging_test_fixture.json', ] + + def setUp(self): + self.user = User.objects.get(pk=1) + + def test_string_representation(self): + expected = "This is a title" + p1 = Post(title=expected) + actual = str(p1) + self.assertEqual(expected, actual) + class CategoryTestCase(TestCase): def test_string_representation(self): diff --git a/test_results_blogging.txt b/test_results_blogging.txt index 8d4a461..5cb0cab 100644 --- a/test_results_blogging.txt +++ b/test_results_blogging.txt @@ -1,8 +1,3 @@ -.. ----------------------------------------------------------------------- -Ran 2 tests in 0.090s - -OK Creating test database for alias 'default'... System check identified no issues (0 silenced). Destroying test database for alias 'default'... From f44e6d3a649839ee86cf33fae2bc914050d6703d Mon Sep 17 00:00:00 2001 From: mmanc125uw Date: Tue, 29 Sep 2020 14:57:19 -0400 Subject: [PATCH 6/8] sp_py230 lesson7 activity, updates for detail_view --- blogging/tests.py | 2 -- blogging/urls.py | 5 +++-- blogging/views.py | 9 +++++++++ readmeSetupSteps.txt | 4 ++++ test_results_blogging.txt | 5 +++++ 5 files changed, 21 insertions(+), 4 deletions(-) diff --git a/blogging/tests.py b/blogging/tests.py index adbef52..b1fa495 100644 --- a/blogging/tests.py +++ b/blogging/tests.py @@ -59,7 +59,6 @@ def test_list_only_published(self): else: self.assertNotContains(resp, title) - """ def test_details_only_published(self): for count in range(1, 11): title = "Post %d Title" % count @@ -70,4 +69,3 @@ def test_details_only_published(self): self.assertContains(resp, title) else: self.assertEqual(resp.status_code, 404) - """ diff --git a/blogging/urls.py b/blogging/urls.py index 8ca5acf..f3c676c 100644 --- a/blogging/urls.py +++ b/blogging/urls.py @@ -1,11 +1,12 @@ from django.urls import path -from blogging.views import stub_view, list_view +from blogging.views import stub_view, list_view, detail_view urlpatterns = [ ### path('', stub_view, name="blog_index"), path('', list_view, name="blog_index"), - path('posts//', stub_view, name="blog_detail"), +### path('posts//', stub_view, name="blog_detail"), + path('posts//', detail_view, name="blog_detail"), ] diff --git a/blogging/views.py b/blogging/views.py index 99801a9..81b5f73 100644 --- a/blogging/views.py +++ b/blogging/views.py @@ -21,3 +21,12 @@ def list_view(request): context = {'posts': posts} body = template.render(context) return HttpResponse(body, content_type="text/html") + +def detail_view(request, post_id): + published = Post.objects.exclude(published_date__exact=None) + try: + post = published.get(pk=post_id) + except Post.DoesNotExist: + raise Http404 + context = {'post': post} + return render(request, 'blogging/detail.html', context) \ No newline at end of file diff --git a/readmeSetupSteps.txt b/readmeSetupSteps.txt index 17c270d..199264d 100644 --- a/readmeSetupSteps.txt +++ b/readmeSetupSteps.txt @@ -86,6 +86,10 @@ add this to misite/misite/urls.py ; path('', include('blogging.urls')), edit blogging/tests.py ; per lesson add test_list_only_published edit blogging/templates/blogging/list.html ; per lesson, list html edit blogging/view.html ; per lesson, add list view +edit blogging/templates/blogging/detail.html ; per lesson, detail html +edit blogging/detail.html ; per lesson, add detail view +edit blogging/templates/blogging/list.html ; per lesson, 4 tests run successfully + diff --git a/test_results_blogging.txt b/test_results_blogging.txt index 5cb0cab..9261494 100644 --- a/test_results_blogging.txt +++ b/test_results_blogging.txt @@ -1,3 +1,8 @@ +.... +---------------------------------------------------------------------- +Ran 4 tests in 0.124s + +OK Creating test database for alias 'default'... System check identified no issues (0 silenced). Destroying test database for alias 'default'... From f61c465abcf881178c02418d581b2db0ff88e942 Mon Sep 17 00:00:00 2001 From: mmanc125uw Date: Tue, 29 Sep 2020 15:09:18 -0400 Subject: [PATCH 7/8] sp_py230 lesson7 activity, static files, css --- mysite/templates/base.html | 2 ++ readmeSetupSteps.txt | 2 ++ 2 files changed, 4 insertions(+) diff --git a/mysite/templates/base.html b/mysite/templates/base.html index 2eacafa..bf5506f 100644 --- a/mysite/templates/base.html +++ b/mysite/templates/base.html @@ -1,7 +1,9 @@ +{% load staticfiles %} My Django Blog +
diff --git a/readmeSetupSteps.txt b/readmeSetupSteps.txt index 199264d..1015f5f 100644 --- a/readmeSetupSteps.txt +++ b/readmeSetupSteps.txt @@ -89,6 +89,8 @@ edit blogging/view.html ; per lesson, add list view edit blogging/templates/blogging/detail.html ; per lesson, detail html edit blogging/detail.html ; per lesson, add detail view edit blogging/templates/blogging/list.html ; per lesson, 4 tests run successfully +edit mysite/templates/base.html ; per lesson, static files, css + From badd99fa4ece53bbb90c39c73f6d5b874ea33200 Mon Sep 17 00:00:00 2001 From: mmanc125uw Date: Tue, 29 Sep 2020 15:49:42 -0400 Subject: [PATCH 8/8] sp_py230 lesson7 activity, updated for login template --- mysite/settings.py | 3 +++ mysite/templates/base.html | 10 ++++++++++ mysite/urls.py | 4 ++++ readmeSetupSteps.txt | 8 ++++++++ 4 files changed, 25 insertions(+) diff --git a/mysite/settings.py b/mysite/settings.py index 65a7bdc..a96acbc 100644 --- a/mysite/settings.py +++ b/mysite/settings.py @@ -120,3 +120,6 @@ # https://docs.djangoproject.com/en/2.1/howto/static-files/ STATIC_URL = '/static/' + +LOGIN_URL = '/login/' +LOGIN_REDIRECT_URL = '/' diff --git a/mysite/templates/base.html b/mysite/templates/base.html index bf5506f..1ab48a3 100644 --- a/mysite/templates/base.html +++ b/mysite/templates/base.html @@ -6,6 +6,16 @@ + {# header ends here #}
{% block content %} diff --git a/mysite/urls.py b/mysite/urls.py index d5b24b3..23c3b23 100644 --- a/mysite/urls.py +++ b/mysite/urls.py @@ -2,10 +2,14 @@ from django.contrib import admin from django.urls import path, include # <-- Make sure you have both of these imports. +from django.contrib.auth.views import LoginView, LogoutView + urlpatterns = [ path('', include('blogging.urls')), # <-- add this path('polling/', include('polling.urls')), # <-- Add this path('blogging/', include('blogging.urls')), # <-- Add this path('admin/', admin.site.urls), + path('login/', LoginView.as_view(template_name='login.html'), name="login"), + path('logout/', LogoutView.as_view(next_page='/'), name="logout"), ] diff --git a/readmeSetupSteps.txt b/readmeSetupSteps.txt index 1015f5f..0d72218 100644 --- a/readmeSetupSteps.txt +++ b/readmeSetupSteps.txt @@ -90,6 +90,14 @@ edit blogging/templates/blogging/detail.html ; per lesson, detail html edit blogging/detail.html ; per lesson, add detail view edit blogging/templates/blogging/list.html ; per lesson, 4 tests run successfully edit mysite/templates/base.html ; per lesson, static files, css +edit mysite/templates/login.html ; per lesson, for login +edit mysite/mysite/urls.py ; per lesson, for login, logout +http://localhost:8000/admin ; test login page + + + + +