diff --git a/blogging/admin.py b/blogging/admin.py index 3db7856..385d0fe 100644 --- a/blogging/admin.py +++ b/blogging/admin.py @@ -1,5 +1,5 @@ +# blogging/admin.py from django.contrib import admin from blogging.models import Post admin.site.register(Post) - 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/models.py b/blogging/models.py index 6b49f00..eb0df86 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): @@ -8,6 +8,3 @@ class Post(models.Model): created_date = models.DateTimeField(auto_now_add=True) modified_date = models.DateTimeField(auto_now=True) published_date = models.DateTimeField(blank=True, null=True) - - def __str__(self): - return self.title diff --git a/blogging/tests.py b/blogging/tests.py index 03b08d1..7ce503c 100644 --- a/blogging/tests.py +++ b/blogging/tests.py @@ -1,17 +1,3 @@ from django.test import TestCase -from django.contrib.auth.models import User -from blogging.models import Post - - -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) +# Create your tests here. diff --git a/blogging/urls.py b/blogging/urls.py new file mode 100644 index 0000000..dce7aa0 --- /dev/null +++ b/blogging/urls.py @@ -0,0 +1,9 @@ +# polling/urls.py + +from django.urls import path +from blogging.views import list_view, detail_view + +urlpatterns = [ + path('', list_view, name="poll_index"), + path('polls//', detail_view, name="poll_detail"), +] diff --git a/blogging/views.py b/blogging/views.py index 91ea44a..6339808 100644 --- a/blogging/views.py +++ b/blogging/views.py @@ -1,3 +1,23 @@ from django.shortcuts import render +from django.http import Http404 +from polling.models import Poll -# Create your views here. +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) 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 cddc23e..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': [ diff --git a/mysite/urls.py b/mysite/urls.py index c52489c..903e7bd 100644 --- a/mysite/urls.py +++ b/mysite/urls.py @@ -1,22 +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.urls import path, include # <-- Make sure you have both of these imports. + urlpatterns = [ - path('polling/', include('polling.urls')), + path('polling/', include('polling.urls')), # <-- Add this + path('blogging/', include('blogging.urls')), # <-- Add this path('admin/', admin.site.urls), ] 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..40fb325 --- /dev/null +++ b/readmeSetupSteps.txt @@ -0,0 +1,80 @@ + +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 + + + + + + + + + + + + diff --git a/test_results_djangoAdmin_browser_snapshots.docx b/test_results_djangoAdmin_browser_snapshots.docx new file mode 100644 index 0000000..045c1be Binary files /dev/null and b/test_results_djangoAdmin_browser_snapshots.docx differ