Preview
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
from django.contrib import admin
|
||||
from .models import Post
|
||||
|
||||
admin.site.register(Post)
|
||||
@@ -0,0 +1,5 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class BlogConfig(AppConfig):
|
||||
name = 'blog'
|
||||
@@ -0,0 +1,9 @@
|
||||
from django import forms
|
||||
from .models import Post
|
||||
|
||||
|
||||
class PostForm(forms.ModelForm):
|
||||
|
||||
class Meta:
|
||||
model = Post
|
||||
fields = ('title', 'text',)
|
||||
@@ -0,0 +1,29 @@
|
||||
# Generated by Django 2.0.8 on 2018-08-22 09:02
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
import django.utils.timezone
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Post',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('title', models.CharField(max_length=200)),
|
||||
('text', models.TextField()),
|
||||
('created_date', models.DateTimeField(default=django.utils.timezone.now)),
|
||||
('published_date', models.DateTimeField(blank=True, null=True)),
|
||||
('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,22 @@
|
||||
from django.db import models
|
||||
from django.utils import timezone
|
||||
|
||||
|
||||
class Post(models.Model):
|
||||
"""
|
||||
model Post
|
||||
"""
|
||||
author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
|
||||
title = models.CharField(max_length=200)
|
||||
text = models.TextField()
|
||||
created_date = models.DateTimeField(
|
||||
default=timezone.now)
|
||||
published_date = models.DateTimeField(
|
||||
blank=True, null=True)
|
||||
|
||||
def publish(self):
|
||||
self.published_date = timezone.now()
|
||||
self.save()
|
||||
|
||||
def __str__(self):
|
||||
return self.title
|
||||
@@ -0,0 +1,46 @@
|
||||
.page-header {
|
||||
background-color: #ff9400;
|
||||
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 h1 a, .post h1 a:visited {
|
||||
color: #000000;
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
{% load static %}
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>Django Girls blog - Build %%%BUILDTAG%%%</title>
|
||||
<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>
|
||||
<div class="page-header">
|
||||
<div class="top-menu" style="float:right">
|
||||
{% if user.is_authenticated %}
|
||||
<h3> {{ request.user }} | <a href="/admin/logout?next=/">Log out</a> </h3>
|
||||
{% else %}
|
||||
<h3> <a href="/admin/login/?next=/">Log in</a> </h3>
|
||||
{% endif %}
|
||||
{% if user.is_authenticated %}
|
||||
<a href="{% url 'post_new' %}" class="top-menu"><span class="glyphicon glyphicon-plus"></span></a>
|
||||
{% endif %}
|
||||
</div>
|
||||
<h1><a href="/">Django Girls Blog</a></h1>
|
||||
</div>
|
||||
<div class="content container">
|
||||
<div class="row">
|
||||
<div class="col-md-8">
|
||||
{% block content %}
|
||||
{% endblock %}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -0,0 +1,19 @@
|
||||
{% extends 'blog/base.html' %}
|
||||
|
||||
{% block content %}
|
||||
<div class="post">
|
||||
{% if post.published_date %}
|
||||
<div class="date">
|
||||
{{ post.published_date }}
|
||||
</div>
|
||||
{% endif %}
|
||||
{% if user.is_authenticated %}
|
||||
<a class="btn btn-default" href="{% url 'post_edit' pk=post.pk %}"><span class="glyphicon glyphicon-pencil"></span></a>
|
||||
{% endif %}
|
||||
<h1>{{ post.title }}</h1>
|
||||
<p>{{ post.text|linebreaksbr }}</p>
|
||||
</div>
|
||||
<div>
|
||||
<h4><a href="/">Back</a></h4>
|
||||
</div>
|
||||
{% endblock %}
|
||||
@@ -0,0 +1,12 @@
|
||||
{% extends 'blog/base.html' %}
|
||||
|
||||
{% block content %}
|
||||
<h1>New post</h1>
|
||||
<form method="POST" class="post-form">{% csrf_token %}
|
||||
{{ form.as_p }}
|
||||
<button type="submit" class="save btn btn-default">Save</button>
|
||||
</form>
|
||||
<div>
|
||||
<h4><a href="/">Back</a></h4>
|
||||
</div>
|
||||
{% endblock %}
|
||||
@@ -0,0 +1,13 @@
|
||||
{% extends 'blog/base.html' %}
|
||||
|
||||
{% block content %}
|
||||
{% for post in posts %}
|
||||
<div class="post">
|
||||
<div class="date">
|
||||
{{ post.published_date }}
|
||||
</div>
|
||||
<h1><a href="{% url 'post_detail' pk=post.pk %}">{{ post.title }}</a></h1>
|
||||
<p>{{ post.text|linebreaksbr }}</p>
|
||||
</div>
|
||||
{% endfor %}
|
||||
{% endblock %}
|
||||
@@ -0,0 +1,41 @@
|
||||
from django.contrib.auth.models import User
|
||||
from django.test import TestCase
|
||||
from django.utils import timezone
|
||||
|
||||
from .models import Post
|
||||
from .forms import PostForm
|
||||
|
||||
|
||||
class PostTestCase(TestCase):
|
||||
def setUp(self):
|
||||
self.user1 = User.objects.create_user(username="admin")
|
||||
Post.objects.create(author=self.user1,
|
||||
title="Test",
|
||||
text="We are testing this",
|
||||
created_date=timezone.now(),
|
||||
published_date=timezone.now())
|
||||
|
||||
def test_post_is_posted(self):
|
||||
"""Posts are created"""
|
||||
post1 = Post.objects.get(title="Test")
|
||||
self.assertEqual(post1.text, "We are testing this")
|
||||
|
||||
def test_valid_form_data(self):
|
||||
form = PostForm({
|
||||
'title': "Just testing",
|
||||
'text': "Repeated tests make the app foul-proof",
|
||||
})
|
||||
self.assertTrue(form.is_valid())
|
||||
post1 = form.save(commit=False)
|
||||
post1.author = self.user1
|
||||
post1.save()
|
||||
self.assertEqual(post1.title, "Just testing")
|
||||
self.assertEqual(post1.text, "Repeated tests make the app foul-proof")
|
||||
|
||||
def test_blank_form_data(self):
|
||||
form = PostForm({})
|
||||
self.assertFalse(form.is_valid())
|
||||
self.assertEqual(form.errors, {
|
||||
'title': ['This field is required.'],
|
||||
'text': ['This field is required.'],
|
||||
})
|
||||
@@ -0,0 +1,9 @@
|
||||
from django.urls import path
|
||||
from . import views
|
||||
|
||||
urlpatterns = [
|
||||
path('', views.post_list, name='post_list'),
|
||||
path('post/<int:pk>/', views.post_detail, name='post_detail'),
|
||||
path('post/new', views.post_new, name='post_new'),
|
||||
path('post/<int:pk>/edit/', views.post_edit, name='post_edit'),
|
||||
]
|
||||
@@ -0,0 +1,44 @@
|
||||
from django.shortcuts import render, get_object_or_404, redirect
|
||||
from django.utils import timezone
|
||||
from .models import Post
|
||||
from .forms import PostForm
|
||||
|
||||
|
||||
def post_list(request):
|
||||
posts = Post.objects.filter(published_date__lte=timezone.now()).order_by(
|
||||
'published_date')
|
||||
return render(request, 'blog/post_list.html', {'posts': posts})
|
||||
|
||||
|
||||
def post_detail(request, pk):
|
||||
post = get_object_or_404(Post, pk=pk)
|
||||
return render(request, 'blog/post_detail.html', {'post': post})
|
||||
|
||||
|
||||
def post_new(request):
|
||||
if request.method == "POST":
|
||||
form = PostForm(request.POST)
|
||||
if form.is_valid():
|
||||
post = form.save(commit=False)
|
||||
post.author = request.user
|
||||
post.published_date = timezone.now()
|
||||
post.save()
|
||||
return redirect('post_detail', pk=post.pk)
|
||||
else:
|
||||
form = PostForm()
|
||||
return render(request, 'blog/post_edit.html', {'form': form})
|
||||
|
||||
|
||||
def post_edit(request, pk):
|
||||
post = get_object_or_404(Post, pk=pk)
|
||||
if request.method == "POST":
|
||||
form = PostForm(request.POST, instance=post)
|
||||
if form.is_valid():
|
||||
post = form.save(commit=False)
|
||||
post.author = request.user
|
||||
post.published_date = timezone.now()
|
||||
post.save()
|
||||
return redirect('post_detail', pk=post.pk)
|
||||
else:
|
||||
form = PostForm(instance=post)
|
||||
return render(request, 'blog/post_edit.html', {'form': form})
|
||||
Reference in New Issue
Block a user