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

finished lesson 07 activity #6

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
3 changes: 0 additions & 3 deletions .gitignore

This file was deleted.

Binary file added blogging/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file added blogging/__pycache__/admin.cpython-36.pyc
Binary file not shown.
Binary file added blogging/__pycache__/models.cpython-36.pyc
Binary file not shown.
Binary file added blogging/__pycache__/tests.cpython-36.pyc
Binary file not shown.
Binary file added blogging/__pycache__/urls.cpython-36.pyc
Binary file not shown.
Binary file added blogging/__pycache__/views.cpython-36.pyc
Binary file not shown.
4 changes: 2 additions & 2 deletions blogging/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.contrib import admin
from blogging.models import Post, Category

from blogging.models import Post, Category

admin.site.register(Post)
admin.site.register(Category)
admin.site.register(Category)
2 changes: 1 addition & 1 deletion blogging/fixtures/blogging_test_fixture.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@
"date_joined": "2013-05-24T05:35:58.628Z"
}
}
]
]
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.1 on 2020-05-18 15:31

from django.conf import settings
from django.db import migrations, models
Expand Down
2 changes: 1 addition & 1 deletion blogging/migrations/0002_category.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 2.1.1 on 2019-11-05 03:35
# Generated by Django 2.1.1 on 2020-05-19 15:23

from django.db import migrations, models

Expand Down
17 changes: 0 additions & 17 deletions blogging/migrations/0003_auto_20191104_1942.py

This file was deleted.

Binary file not shown.
Binary file not shown.
Binary file not shown.
14 changes: 8 additions & 6 deletions blogging/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,25 @@
from django.contrib.auth.models import User

class Post(models.Model):

title = models.CharField(max_length=128)
text = models.TextField(blank=True)
author = models.ForeignKey(User, on_delete=models.CASCADE)
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

class Category(models.Model):

name = models.CharField(max_length=128)
description = models.TextField(blank=True)
posts = models.ManyToManyField(Post, blank=True, related_name='categories')

class Meta:
verbose_name_plural = 'Categories'


def __str__(self):
return self.name

class Meta:
verbose_name_plural = 'Categories'
2 changes: 1 addition & 1 deletion blogging/static/django_blog.css
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,4 @@ ul.categories {
}
ul.categories li {
display: inline;
}
}
2 changes: 1 addition & 1 deletion blogging/templates/blogging/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ <h1>{{ post }}</h1>
<li>{{ category }}</li>
{% endfor %}
</ul>
{% endblock %}
{% endblock %}
40 changes: 20 additions & 20 deletions blogging/templates/blogging/list.html
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
{% extends "base.html" %}{% block content %}
<h1>Recent Posts</h1>
{% comment %} here is where the query happens {% endcomment %}
{% for post in posts %}
<div class="post">
<h2>
<a href="{% url 'blog_detail' post.pk %}">{{ post }}</a>
</h2>
<p class="byline">
Posted by {{ post.author.username }} &mdash; {{ post.published_date }}
</p>
<div class="post-body">
{{ post.text }}
<h1>Recent Posts</h1>
{% comment %} here is where the query happens {% endcomment %}
{% for post in posts %}
<div class="post">
<h2>
<a href="{% url "blog_detail" post.pk %}">{{ post }}</a>
</h2>
<p class="byline">
Posted by {{ post.author.username }} &mdash; {{ post.published_date }}
</p>
<div class="post-body">
{{ post.text }}
</div>
<ul class="categories">
{% for category in post.categories.all %}
<li>{{ category }}</li>
{% endfor %}
</ul>
</div>
<ul class="categories">
{% for category in post.categories.all %}
<li>{{ category }}</li>
{% endfor %}
</ul>
</div>
{% endfor %}
{% endblock %}
{% endfor %}
{% endblock %}
20 changes: 8 additions & 12 deletions blogging/tests.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,31 @@
import datetime

from django.utils.timezone import utc
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', ]
Expand All @@ -47,7 +43,7 @@ def setUp(self):
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
Expand All @@ -59,7 +55,7 @@ def test_list_only_published(self):
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
Expand All @@ -69,4 +65,4 @@ def test_details_only_published(self):
self.assertEqual(resp.status_code, 200)
self.assertContains(resp, title)
else:
self.assertEqual(resp.status_code, 404)
self.assertEqual(resp.status_code, 404)
4 changes: 2 additions & 2 deletions blogging/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
from blogging.views import list_view, detail_view

urlpatterns = [
path('', list_view, name="blog_index"),
path('',list_view, name="blog_index"),
path('posts/<int:post_id>/', detail_view, name="blog_detail"),
]
]
29 changes: 19 additions & 10 deletions blogging/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.template import loader

from blogging.models import Post
from django.shortcuts import render

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")

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)

def detail_view(request, post_id):
published = Post.objects.exclude(published_date__exact=None)
Expand All @@ -12,11 +28,4 @@ def detail_view(request, post_id):
except Post.DoesNotExist:
raise Http404
context = {'post': post}
return render(request, 'blogging/detail.html', context)


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)
return render(request, 'blogging/detail.html', context)
Binary file added db.sqlite3
Binary file not shown.
Binary file added mysite/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file added mysite/__pycache__/settings.cpython-36.pyc
Binary file not shown.
Binary file added mysite/__pycache__/urls.cpython-36.pyc
Binary file not shown.
Binary file added mysite/__pycache__/wsgi.cpython-36.pyc
Binary file not shown.
3 changes: 1 addition & 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 = '9@s4f=3pgo)l2(#*zoogaurq$#v*%bi1d9cmim1(z(2budl@!%'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
Expand Down Expand Up @@ -120,6 +120,5 @@
# https://docs.djangoproject.com/en/2.1/howto/static-files/

STATIC_URL = '/static/'

LOGIN_URL = '/login/'
LOGIN_REDIRECT_URL = '/'
51 changes: 27 additions & 24 deletions mysite/templates/base.html
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
{% load staticfiles %}

<!DOCTYPE html>
<html>
<head>
<title>My Django Blog</title>
<link type="text/css" rel="stylesheet" href="{% static 'django_blog.css' %}">
</head>
<body>
<div id="header"> {# header begins here #}
<ul id="control-bar">
{% if user.is_authenticated %}
{% if user.is_staff %}<li><a href="{% url 'admin:index' %}">admin</a></li>{% endif %}
<li><a href="{% url 'logout' %}">logout</a></li>
{% else %}
<li><a href="{% url 'login' %}">login</a></li>
{% endif %}
</ul>
</div> {# header ends here #}
<div id="container">
<div id="content">
{% block content %}
[content will go here]
{% endblock %}
</div>
</div>
</body>
</html>
<head>
<title>My Django Blog</title>
<link type="text/css" rel="stylesheet" href="{% static 'django_blog.css' %}">
</head>
<body>

<div id="header"> {# header begins here #}
<ul id="control-bar">
{% if user.is_authenticated %}
{% if user.is_staff %}<li><a href="{% url 'admin:index' %}">admin</a></li>{% endif %}
<li><a href="{% url 'logout' %}">logout</a></li>
{% else %}
<li><a href="{% url 'login' %}">login</a></li>
{% endif %}
</ul>
</div>

<div id="container">
<div id="content">
{% block content %}
[content will go here]
{% endblock %}
</div>
</div>
</body>
</html>`
2 changes: 1 addition & 1 deletion mysite/templates/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ <h1>My Blog Login</h1>
{{ form.as_p }}
<p><input type="submit" value="Log In"></p>
</form>
{% endblock %}
{% endblock %}
5 changes: 2 additions & 3 deletions mysite/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
from django.urls import path, include
from django.contrib.auth.views import LoginView, LogoutView


urlpatterns = [
path('', include('blogging.urls')),
path('polling/', include('polling.urls')),
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"),
path("login/", LoginView.as_view(template_name="login.html"), name="login"),
path("logout/", LogoutView.as_view(next_page="/"), name="logout"),
]
Binary file added polling/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file added polling/__pycache__/admin.cpython-36.pyc
Binary file not shown.
Binary file added polling/__pycache__/models.cpython-36.pyc
Binary file not shown.
Binary file added polling/__pycache__/urls.cpython-36.pyc
Binary file not shown.
Binary file added polling/__pycache__/views.cpython-36.pyc
Binary file not shown.
3 changes: 2 additions & 1 deletion polling/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# blogging/admin.py
from django.contrib import admin
from polling.models import Poll

admin.site.register(Poll)
admin.site.register(Poll)
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-05-18 14:54

from django.db import migrations, models

Expand Down
Binary file not shown.
Binary file not shown.
3 changes: 2 additions & 1 deletion 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 All @@ -6,4 +7,4 @@ class Poll(models.Model):
score = models.IntegerField(default=0)

def __str__(self):
return self.title
return self.title
2 changes: 1 addition & 1 deletion polling/templates/polling/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ <h1>{{ poll.title }}</h1>
<input type="submit" name="vote" value="No">
</form>
</div>
{% endblock %}
{% endblock %}
4 changes: 3 additions & 1 deletion 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 All @@ -8,4 +10,4 @@ <h2>
</h2>
</div>
{% endfor %}
{% endblock %}
{% endblock %}
Loading