Skip to content

solution to assignment 7 #2

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

Open
wants to merge 5 commits 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
File renamed without changes.
22 changes: 22 additions & 0 deletions blogging/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from django.contrib import admin

from blogging.models import Post, Category, ModelAdmin2


class ModelAdminClass(admin.TabularInline):
model = ModelAdmin2
extra = 1


class PostAdmin(admin.ModelAdmin):
inlines = (ModelAdminClass,)


class CategoryAdmin(admin.ModelAdmin):
exclude = ('posts',)


# and a new admin registration
admin.site.register(Post, PostAdmin)
admin.site.register(Category, CategoryAdmin)
# Register your models here.
5 changes: 5 additions & 0 deletions blogging/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class BloggingConfig(AppConfig):
name = 'blogging'
7 changes: 7 additions & 0 deletions blogging/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.forms import ModelForm
from blogging.models import Post

class PostForm(ModelForm):
class Meta:
model = Post
fields = ['title', 'text']
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 2.1.1 on 2018-10-14 18:36
# Generated by Django 2.1.1 on 2019-10-30 16:31

from django.conf import settings
from django.db import migrations, models
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Generated by Django 2.1.1 on 2018-10-18 18:24
# Generated by Django 2.1.1 on 2019-10-31 12:22

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('myblog', '0001_initial'),
('blogging', '0001_initial'),
]

operations = [
Expand All @@ -16,7 +16,7 @@ class Migration(migrations.Migration):
('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='myblog.Post')),
('posts', models.ManyToManyField(blank=True, related_name='categories', to='blogging.Post')),
],
),
]
34 changes: 34 additions & 0 deletions blogging/migrations/0003_auto_20191031_1814.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Generated by Django 2.1.1 on 2019-10-31 18:14

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('blogging', '0002_category'),
]

operations = [
migrations.CreateModel(
name='ModelAdmin2',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
],
),
migrations.AlterModelOptions(
name='category',
options={'verbose_name_plural': 'Categories'},
),
migrations.AddField(
model_name='modeladmin2',
name='category',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='blogging.Category'),
),
migrations.AddField(
model_name='modeladmin2',
name='post',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='blogging.Post'),
),
]
File renamed without changes.
12 changes: 10 additions & 2 deletions myblog/models.py → blogging/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from django.db import models
from django.db import models # <-- This is already in the file
from django.contrib.auth.models import User
from django.urls import reverse


class Post(models.Model):
title = models.CharField(max_length=128)
Expand All @@ -9,6 +11,9 @@ class Post(models.Model):
modified_date = models.DateTimeField(auto_now=True)
published_date = models.DateTimeField(blank=True, null=True)

def get_absolute_url(self):
return reverse('blogging:detail', kwargs={'pk':self.pk})

def __str__(self):
return self.title

Expand All @@ -18,8 +23,11 @@ class Category(models.Model):
posts = models.ManyToManyField(Post, blank=True, related_name='categories')

class Meta:
verbose_name_plural = 'Categories'
verbose_name_plural = 'Categories'

def __str__(self):
return self.name

class ModelAdmin2(models.Model):
post = models.ForeignKey(Post, on_delete=models.CASCADE)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
56 changes: 56 additions & 0 deletions blogging/static/css/blog.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
h1 a, h2 a {
color: #C25100;
font-family: 'Lobster';
}

body {
padding-left: 15px;
}


.page-header {
background-color: #C25100;
margin-top: 0;
padding: 20px 20px 20px 40px;
}

.page-header h1, .page-header h1 a, .page-header h1 a:visited, .page-header h1 a:active {
color: #ffffff;
font-size: 36pt;
text-decoration: none;
}

.content {
margin-left: 40px;
}

h1, h2, h3, h4 {
font-family: 'Lobster', cursive;
}

.date {
color: #828282;
}

.save {
float: right;
}

.post-form textarea, .post-form input {
width: 100%;
}

.top-menu, .top-menu:hover, .top-menu:visited {
color: #ffffff;
float: right;
font-size: 26pt;
margin-right: 20px;
}

.post {
margin-bottom: 70px;
}

.post h2 a, .post h2 a:visited {
color: #000000;
}
148 changes: 74 additions & 74 deletions myblog/static/django_blog.css → blogging/static/django_blog.css
Original file line number Diff line number Diff line change
@@ -1,74 +1,74 @@
body {
background-color: #eee;
color: #111;
font-family: "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif;
margin:0;
padding:0;
}
#container {
margin:0;
padding:0;
margin-top: 0px;
}
#header {
background-color: #333;
border-botton: 1px solid #111;
margin:0;
padding:0;
}
#control-bar {
margin: 0em 0em 1em;
list-style: none;
list-style-type: none;
text-align: right;
color: #eee;
font-size: 80%;
padding-bottom: 0.4em;
}
#control-bar li {
display: inline-block;
}
#control-bar li a {
color: #eee;
padding: 0.5em;
text-decoration: none;
}
#control-bar li a:hover {
color: #cce;
}
#content {
margin: 0em 1em 1em;
}

ul#entries {
list-style: none;
list-style-type: none;
}
div.entry {
margin-right: 2em;
margin-top: 1em;
border-top: 1px solid #cecece;
}
ul#entries li:first-child div.entry {
border-top: none;
margin-top: 0em;
}
div.entry-body {
margin-left: 2em;
}
.notification {
float: right;
text-align: center;
width: 25%;
padding: 1em;
}
.info {
background-color: #aae;
}
ul.categories {
list-style: none;
list-style-type: none;
}
ul.categories li {
display: inline;
}
body {
background-color: #eee;
color: #111;
font-family: "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif;
margin:0;
padding:0;
}
#container {
margin:0;
padding:0;
margin-top: 0px;
}
#header {
background-color: #333;
border-botton: 1px solid #111;
margin:0;
padding:0;
}
#control-bar {
margin: 0em 0em 1em;
list-style: none;
list-style-type: none;
text-align: right;
color: #eee;
font-size: 80%;
padding-bottom: 0.4em;
}
#control-bar li {
display: inline-block;
}
#control-bar li a {
color: #eee;
padding: 0.5em;
text-decoration: none;
}
#control-bar li a:hover {
color: #cce;
}
#content {
margin: 0em 1em 1em;
}
ul#entries {
list-style: none;
list-style-type: none;
}
div.entry {
margin-right: 2em;
margin-top: 1em;
border-top: 1px solid #cecece;
}
ul#entries li:first-child div.entry {
border-top: none;
margin-top: 0em;
}
div.entry-body {
margin-left: 2em;
}
.notification {
float: right;
text-align: center;
width: 25%;
padding: 1em;
}
.info {
background-color: #aae;
}
ul.categories {
list-style: none;
list-style-type: none;
}
ul.categories li {
display: inline;
}
27 changes: 27 additions & 0 deletions blogging/templates/blogging/detail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{% extends "base.html" %}
{% load static %}
{% block content %}
<html>
<head>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
<link href="//fonts.googleapis.com/css?family=Lobster&subset=latin,latin-ext" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="{% static 'css/blog.css' %}">
</head>
<body>
<h1><a class="backlink" href="/">Home</a></h1>
<h1>{{ post }}</h1>
<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>
{% endblock %}
</body>
</html>
Loading