diff --git a/.DS_Store b/.DS_Store
new file mode 100644
index 0000000..2f87511
Binary files /dev/null and b/.DS_Store differ
diff --git a/blogging/.DS_Store b/blogging/.DS_Store
new file mode 100644
index 0000000..7ccf0d0
Binary files /dev/null and b/blogging/.DS_Store differ
diff --git a/blogging/admin.py b/blogging/admin.py
index 1175916..2715183 100644
--- a/blogging/admin.py
+++ b/blogging/admin.py
@@ -2,5 +2,15 @@
from blogging.models import Post, Category
-admin.site.register(Post)
-admin.site.register(Category)
+@admin.register(Category)
+class CategoryAdmin(admin.ModelAdmin):
+ exclude = ('posts',)
+
+
+class CategoryInline(admin.TabularInline):
+ model = Category.posts.through
+
+
+@admin.register(Post)
+class PostAdmin(admin.ModelAdmin):
+ inlines = [CategoryInline]
diff --git a/blogging/fixtures/blogging_test_fixture.json b/blogging/fixtures/blogging_test_fixture.json
index bf5269e..e384e53 100644
--- a/blogging/fixtures/blogging_test_fixture.json
+++ b/blogging/fixtures/blogging_test_fixture.json
@@ -1,38 +1,38 @@
-[
- {
- "pk": 1,
- "model": "auth.user",
- "fields": {
- "username": "admin",
- "first_name": "Mr.",
- "last_name": "Administrator",
- "is_active": true,
- "is_superuser": true,
- "is_staff": true,
- "last_login": "2013-05-24T05:35:58.628Z",
- "groups": [],
- "user_permissions": [],
- "password": "pbkdf2_sha256$10000$1rQazFNdOfFt$6aw/uIrv2uASkZ7moXMTajSN+ySYuowBnbP6ILNQntE=",
- "email": "admin@example.com",
- "date_joined": "2013-05-24T05:35:58.628Z"
- }
- },
- {
- "pk": 2,
- "model": "auth.user",
- "fields": {
- "username": "noname",
- "first_name": "",
- "last_name": "",
- "is_active": true,
- "is_superuser": true,
- "is_staff": true,
- "last_login": "2013-05-24T05:35:58.628Z",
- "groups": [],
- "user_permissions": [],
- "password": "pbkdf2_sha256$10000$1rQazFNdOfFt$6aw/uIrv2uASkZ7moXMTajSN+ySYuowBnbP6ILNQntE=",
- "email": "noname@example.com",
- "date_joined": "2013-05-24T05:35:58.628Z"
- }
- }
-]
+[
+ {
+ "pk": 1,
+ "model": "auth.user",
+ "fields": {
+ "username": "admin",
+ "first_name": "Mr.",
+ "last_name": "Administrator",
+ "is_active": true,
+ "is_superuser": true,
+ "is_staff": true,
+ "last_login": "2013-05-24T05:35:58.628Z",
+ "groups": [],
+ "user_permissions": [],
+ "password": "pbkdf2_sha256$10000$1rQazFNdOfFt$6aw/uIrv2uASkZ7moXMTajSN+ySYuowBnbP6ILNQntE=",
+ "email": "admin@example.com",
+ "date_joined": "2013-05-24T05:35:58.628Z"
+ }
+ },
+ {
+ "pk": 2,
+ "model": "auth.user",
+ "fields": {
+ "username": "noname",
+ "first_name": "",
+ "last_name": "",
+ "is_active": true,
+ "is_superuser": true,
+ "is_staff": true,
+ "last_login": "2013-05-24T05:35:58.628Z",
+ "groups": [],
+ "user_permissions": [],
+ "password": "pbkdf2_sha256$10000$1rQazFNdOfFt$6aw/uIrv2uASkZ7moXMTajSN+ySYuowBnbP6ILNQntE=",
+ "email": "noname@example.com",
+ "date_joined": "2013-05-24T05:35:58.628Z"
+ }
+ }
+]
diff --git a/blogging/migrations/0001_initial.py b/blogging/migrations/0001_initial.py
index 5d406bf..5b69ac2 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.1 on 2020-12-19 00:22
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..e1bd005 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.1 on 2020-12-21 19:42
from django.db import migrations, models
@@ -13,10 +13,13 @@ class Migration(migrations.Migration):
migrations.CreateModel(
name='Category',
fields=[
- ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+ ('id', models.AutoField(auto_created=True, primary_key=True,
+ serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=128)),
('description', models.TextField(blank=True)),
- ('posts', models.ManyToManyField(blank=True, related_name='categories', to='blogging.Post')),
+ ('posts', models.ManyToManyField(blank=True,
+ related_name='categories',
+ to='blogging.Post')),
],
),
]
diff --git a/blogging/migrations/0003_auto_20191104_1942.py b/blogging/migrations/0003_auto_20201222_0221.py
similarity index 86%
rename from blogging/migrations/0003_auto_20191104_1942.py
rename to blogging/migrations/0003_auto_20201222_0221.py
index 663c56d..725cd3d 100644
--- a/blogging/migrations/0003_auto_20191104_1942.py
+++ b/blogging/migrations/0003_auto_20201222_0221.py
@@ -1,4 +1,4 @@
-# Generated by Django 2.1.1 on 2019-11-05 03:42
+# Generated by Django 2.1.1 on 2020-12-22 02:21
from django.db import migrations
diff --git a/blogging/models.py b/blogging/models.py
index 10d6cc3..807caa2 100644
--- a/blogging/models.py
+++ b/blogging/models.py
@@ -1,7 +1,11 @@
from django.db import models
from django.contrib.auth.models import User
+
class Post(models.Model):
+ def __str__(self):
+ return self.title
+
title = models.CharField(max_length=128)
text = models.TextField(blank=True)
author = models.ForeignKey(User, on_delete=models.CASCADE)
@@ -9,16 +13,14 @@ class Post(models.Model):
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')
+ def __str__(self):
+ return self.name
class Meta:
verbose_name_plural = 'Categories'
- def __str__(self):
- return self.name
+ name = models.CharField(max_length=128)
+ description = models.TextField(blank=True)
+ posts = models.ManyToManyField(Post, blank=True, related_name='categories')
diff --git a/blogging/static/django_blog.css b/blogging/static/django_blog.css
index 45a882d..303f14b 100644
--- a/blogging/static/django_blog.css
+++ b/blogging/static/django_blog.css
@@ -1,7 +1,7 @@
body {
background-color: #eee;
color: #111;
- font-family: "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif;
+ font-family: 'HelveticaNeue-Light', 'Helvetica Neue Light', 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif;
margin:0;
padding:0;
}
diff --git a/blogging/templates/.DS_Store b/blogging/templates/.DS_Store
new file mode 100644
index 0000000..adbf147
Binary files /dev/null and b/blogging/templates/.DS_Store differ
diff --git a/blogging/templates/blogging/.DS_Store b/blogging/templates/blogging/.DS_Store
new file mode 100644
index 0000000..ecb91dd
Binary files /dev/null and b/blogging/templates/blogging/.DS_Store differ
diff --git a/blogging/templates/blogging/detail.html b/blogging/templates/blogging/detail.html
index ea5b9c8..04c8b63 100644
--- a/blogging/templates/blogging/detail.html
+++ b/blogging/templates/blogging/detail.html
@@ -1,17 +1,17 @@
-{% extends "base.html" %}
+{% extends 'base.html' %}
{% block content %}
-Home
+Home
{{ post }}
-
- Posted by {{ post.author.username }} — {{ post.published_date }}
+
+ Posted by {{ post.author.username }} — {{ post.published_date }}
-
- {{ post.text }}
+
+ {{ post.text }}
-
- {% for category in post.categories.all %}
- - {{ category }}
- {% endfor %}
+
+ {% for category in post.categories.all %}
+ - {{ category }}
+ {% endfor %}
{% endblock %}
diff --git a/blogging/templates/blogging/list.html b/blogging/templates/blogging/list.html
index d8aa919..182d299 100644
--- a/blogging/templates/blogging/list.html
+++ b/blogging/templates/blogging/list.html
@@ -1,22 +1,20 @@
-{% extends "base.html" %}{% block content %}
- Recent Posts
- {% comment %} here is where the query happens {% endcomment %}
- {% for post in posts %}
-
-
-
- Posted by {{ post.author.username }} — {{ post.published_date }}
-
-
- {{ post.text }}
+{% extends 'base.html' %}{% block content %}
+
Recent Posts
+ {% comment %} here is where the query happens {% endcomment %}
+ {% for post in posts %}
+
+
+
+ Posted by {{ post.author.username }} — {{ post.published_date }}
+
+
+ {{ post.text }}
+
+
+ {% for category in post.categories.all %}
+ - {{ category }}
+ {% endfor %}
+
-
- {% for category in post.categories.all %}
- - {{ category }}
- {% endfor %}
-
-
- {% endfor %}
+ {% endfor %}
{% endblock %}
diff --git a/blogging/tests.py b/blogging/tests.py
index 4250226..47124b2 100644
--- a/blogging/tests.py
+++ b/blogging/tests.py
@@ -1,11 +1,8 @@
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
+from blogging.models import Post, Category
class PostTestCase(TestCase):
@@ -15,7 +12,7 @@ def setUp(self):
self.user = User.objects.get(pk=1)
def test_string_representation(self):
- expected = "This is a title"
+ expected = 'This is a title'
p1 = Post(title=expected)
actual = str(p1)
self.assertEqual(expected, actual)
@@ -24,14 +21,14 @@ def test_string_representation(self):
class CategoryTestCase(TestCase):
def test_string_representation(self):
- expected = "A Category"
+ expected = 'A Category'
c1 = Category(name=expected)
actual = str(c1)
self.assertEqual(expected, actual)
class FrontEndTestCase(TestCase):
- """test views provided in the front-end"""
+ """Test views provided in the front-end"""
fixtures = ['blogging_test_fixture.json', ]
def setUp(self):
@@ -39,11 +36,10 @@ def setUp(self):
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",
+ post = Post(title='Post %d Title' % count,
+ text='foo',
author=author)
- if count < 6:
- # publish the first five posts
+ if count < 6: # publish the first five posts
pubdate = self.now - self.timedelta * count
post.published_date = pubdate
post.save()
@@ -52,9 +48,9 @@ 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)
+ self.assertTrue('Recent Posts' in resp_text)
for count in range(1, 11):
- title = "Post %d Title" % count
+ title = 'Post %d Title' % count
if count < 6:
self.assertContains(resp, title, count=1)
else:
@@ -62,7 +58,7 @@ def test_list_only_published(self):
def test_details_only_published(self):
for count in range(1, 11):
- title = "Post %d Title" % count
+ title = 'Post %d Title' % count
post = Post.objects.get(title=title)
resp = self.client.get('/posts/%d/' % post.pk)
if count < 6:
diff --git a/blogging/urls.py b/blogging/urls.py
index 172a39c..d4de3ed 100644
--- a/blogging/urls.py
+++ b/blogging/urls.py
@@ -2,6 +2,6 @@
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='blog_index'),
+ path('posts//', detail_view, name='blog_detail'),
]
diff --git a/blogging/views.py b/blogging/views.py
index b4bab4f..a0eeb98 100644
--- a/blogging/views.py
+++ b/blogging/views.py
@@ -1,10 +1,16 @@
from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.template import loader
-
from blogging.models import Post
+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)
try:
@@ -13,10 +19,3 @@ def detail_view(request, post_id):
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)
diff --git a/manage.py b/manage.py
old mode 100644
new mode 100755
index 390c767..3eb648f
--- a/manage.py
+++ b/manage.py
@@ -8,8 +8,8 @@
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
- "Couldn't import Django. Are you sure it's installed and "
- "available on your PYTHONPATH environment variable? Did you "
- "forget to activate a virtual environment?"
+ 'Couldn\'t import Django. Are you sure it\'s installed and '
+ 'available on your PYTHONPATH environment variable? Did you '
+ 'forget to activate a virtual environment?'
) from exc
execute_from_command_line(sys.argv)
diff --git a/mysite/settings.py b/mysite/settings.py
index 14e4a11..24835e0 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 = 'ijm-qa2dxu0=u4a6d6wge-!3%ndaqoa#)!8%9yogrd*!$c$d@c'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
diff --git a/mysite/templates/base.html b/mysite/templates/base.html
index 4bb0230..f0420f2 100644
--- a/mysite/templates/base.html
+++ b/mysite/templates/base.html
@@ -1,27 +1,28 @@
{% load staticfiles %}
-
- My Django Blog
-
-
-
- {# header ends here #}
-
-
- {% block content %}
- [content will go here]
- {% endblock %}
-
-
-
+
+ My Django Blog
+
+
+
+
+
+
+
+ {% block content %}
+ [content will go here]
+ {% endblock %}
+
+
+
diff --git a/mysite/templates/login.html b/mysite/templates/login.html
index 1566d0f..6ddac16 100644
--- a/mysite/templates/login.html
+++ b/mysite/templates/login.html
@@ -1,9 +1,9 @@
-{% extends "base.html" %}
+{% extends 'base.html' %}
{% block content %}
My Blog Login
-
{% endblock %}
diff --git a/mysite/urls.py b/mysite/urls.py
index 446cd8f..c657ed2 100644
--- a/mysite/urls.py
+++ b/mysite/urls.py
@@ -17,11 +17,11 @@
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('polling/', include('polling.urls')),
+ path('', include('blogging.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/migrations/0001_initial.py b/polling/migrations/0001_initial.py
index 2be60e7..ecb6829 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-12-18 23:41
from django.db import migrations, models
@@ -14,7 +14,8 @@ class Migration(migrations.Migration):
migrations.CreateModel(
name='Poll',
fields=[
- ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+ ('id', models.AutoField(auto_created=True, primary_key=True,
+ serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=128)),
('text', models.TextField(blank=True)),
('score', models.IntegerField(default=0)),
diff --git a/polling/models.py b/polling/models.py
index 6a940d2..97d9967 100644
--- a/polling/models.py
+++ b/polling/models.py
@@ -1,5 +1,6 @@
from django.db import models
+
class Poll(models.Model):
title = models.CharField(max_length=128)
text = models.TextField(blank=True)
diff --git a/polling/templates/polling/detail.html b/polling/templates/polling/detail.html
index bb18db0..025182b 100644
--- a/polling/templates/polling/detail.html
+++ b/polling/templates/polling/detail.html
@@ -1,19 +1,17 @@
-{# polling/templates/polling/detail.html #}
-
-{% extends "base.html" %}
+{% extends 'base.html' %}
{% block content %}
{{ poll.title }}
-
+
{{ poll.text }}
-
+
Current score: {{ poll.score }}
-