Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sp_py230 lesson6 assignment, django poll blog #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion blogging/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# blogging/admin.py
from django.contrib import admin
from blogging.models import Post

admin.site.register(Post)

2 changes: 1 addition & 1 deletion blogging/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 1 addition & 4 deletions blogging/models.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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
16 changes: 1 addition & 15 deletions blogging/tests.py
Original file line number Diff line number Diff line change
@@ -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.
9 changes: 9 additions & 0 deletions blogging/urls.py
Original file line number Diff line number Diff line change
@@ -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/<int:poll_id>/', detail_view, name="poll_detail"),
]
22 changes: 21 additions & 1 deletion blogging/views.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 2 additions & 0 deletions goDjangoSrvStart.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

start python manage.py runserver
4 changes: 2 additions & 2 deletions mysite/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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': [
Expand Down
20 changes: 4 additions & 16 deletions mysite/urls.py
Original file line number Diff line number Diff line change
@@ -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),
]
1 change: 1 addition & 0 deletions polling/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# blogging/admin.py
from django.contrib import admin
from polling.models import Poll

Expand Down
2 changes: 1 addition & 1 deletion polling/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
1 change: 1 addition & 0 deletions polling/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# blogging/models.py
from django.db import models

class Poll(models.Model):
Expand Down
2 changes: 2 additions & 0 deletions polling/templates/polling/list.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{# polling/templates/polling/list.html #}

{% extends "base.html" %}
{% block content %}
<h1>Polls</h1>
Expand Down
2 changes: 2 additions & 0 deletions polling/urls.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# polling/urls.py

from django.urls import path
from polling.views import list_view, detail_view

Expand Down
80 changes: 80 additions & 0 deletions readmeSetupSteps.txt
Original file line number Diff line number Diff line change
@@ -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












Binary file added test_results_djangoAdmin_browser_snapshots.docx
Binary file not shown.