Skip to content

Commit

Permalink
上传第15节课的代码
Browse files Browse the repository at this point in the history
  • Loading branch information
HaddyYang committed Mar 23, 2020
1 parent 94d8f2f commit bca3563
Show file tree
Hide file tree
Showing 37 changed files with 934 additions and 4 deletions.
Empty file.
10 changes: 10 additions & 0 deletions 15.上下篇博客和按月分类/blog/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.contrib import admin
from .models import BlogType, Blog

@admin.register(BlogType)
class BlogTypeAdmin(admin.ModelAdmin):
list_display = ('id', 'type_name')

@admin.register(Blog)
class BlogAdmin(admin.ModelAdmin):
list_display = ('title', 'blog_type', 'author', 'created_time', 'last_updated_time')
5 changes: 5 additions & 0 deletions 15.上下篇博客和按月分类/blog/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class BlogConfig(AppConfig):
name = 'blog'
40 changes: 40 additions & 0 deletions 15.上下篇博客和按月分类/blog/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Generated by Django 2.0 on 2018-10-28 08:58

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


class Migration(migrations.Migration):

initial = True

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name='Blog',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=50)),
('content', models.TextField()),
('created_time', models.DateTimeField(auto_now_add=True)),
('last_updated_time', models.DateTimeField(auto_now=True)),
('author', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to=settings.AUTH_USER_MODEL)),
],
),
migrations.CreateModel(
name='BlogType',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('type_name', models.CharField(max_length=15)),
],
),
migrations.AddField(
model_name='blog',
name='blog_type',
field=models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='blog.BlogType'),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 2.2.5 on 2020-03-19 15:56

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('blog', '0001_initial'),
]

operations = [
migrations.AlterModelOptions(
name='blog',
options={'ordering': ['-created_time']},
),
]
Empty file.
23 changes: 23 additions & 0 deletions 15.上下篇博客和按月分类/blog/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from django.db import models
from django.contrib.auth.models import User

class BlogType(models.Model):
type_name = models.CharField(max_length=15)

def __str__(self):
return self.type_name

class Blog(models.Model):
title = models.CharField(max_length=50)
blog_type = models.ForeignKey(BlogType, on_delete=models.DO_NOTHING)
content = models.TextField()
author = models.ForeignKey(User, on_delete=models.DO_NOTHING)
created_time = models.DateTimeField(auto_now_add=True)
last_updated_time = models.DateTimeField(auto_now=True)

def __str__(self):
return "<Blog: %s>" % self.title

class Meta:
ordering = ['-created_time']

32 changes: 32 additions & 0 deletions 15.上下篇博客和按月分类/blog/static/blog/blog.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
ul.blog-types {
list-style-type: none;
}
div.blog:not(:last-child){
margin-bottom: 2em;
padding-bottom: 1em;
border-bottom: 1px solid #eee;
}
div.blog h3 {
margin-top: 0.5em;
}
div.blog p.blog-info {
margin-top: 0;
}
div.paginator {
text-align: center;
}

ul.blog-info-description {
list-style-type: none;
margin-bottom: 1em;
}
ul.blog-info-description li{
display: inline-block;
margin-right: 2em;
}
div.blog-content{
text-indent: 2em;
}
div.blog-more {
margin-top: 1em;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{% extends 'base.html' %}
{% block title %}{{ blog.title }}{% endblock %}
{% block nav_blog_active %}active{% endblock %}

{% load staticfiles %}
{% block header_extends %}
<link rel="stylesheet" href="{% static 'blog/blog.css' %}">
{% endblock %}

{# 页面内容 #}
{% block content %}
<div class="container">
<div class="row">
<div class="col-xs-10 col-xs-offset-1">
<h3>{{ blog.title }}</h3>
<ul class="blog-info-description">
<li>作者:{{ blog.author }}</li>
<li>分类:<a href="{% url 'blogs_with_type' blog.blog_type.pk %}">{{ blog.blog_type }}</a></li>
<li>发表日期:{{ blog.created_time|date:"Y-m-d H:n:s" }}</li>
</ul>
<div class="blog-content">{{ blog.content }}</div>
<div class="blog-more">
<p>上一篇:
{% if previous_blog %}
<a href="{% url 'blog_detail' previous_blog.pk %}">{{ previous_blog.title }}</a>
{% else %}
没有了
{% endif %}
</p>
<p>下一篇:
{% if next_blog %}
<a href="{% url 'blog_detail' next_blog.pk %}">{{ next_blog.title }}</a>
{% else %}
没有了
{% endif %}
</p>
</div>
</div>
</div>
</div>
{% endblock %}
105 changes: 105 additions & 0 deletions 15.上下篇博客和按月分类/blog/templates/blog/blog_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
{% extends 'base.html' %}
{% block title %}我的网站{% endblock %}
{% block nav_blog_active %}active{% endblock %}

{% load staticfiles %}
{% block header_extends %}
<link rel="stylesheet" href="{% static 'blog/blog.css' %}">
{% endblock %}

{# 页面内容 #}
{% block content %}
<div class="container">
<div class="row">
<div class="col-xs-12 col-sm-8 col-md-9 col-lg-10">
<div class="panel panel-default">
<div class="panel-heading">{% block blog_list_title %}博客列表{% endblock %}</div>
<div class="panel-body">
{% for blog in blogs %}
<div class="blog">
<h3><a href="{% url 'blog_detail' blog.pk %}">{{ blog.title }}</a></h3>
<p class="blog-info">
<span class="glyphicon glyphicon-tag"></span><a href="{% url 'blogs_with_type' blog.blog_type.pk %}">{{ blog.blog_type }}</a>
<span class="glyphicon glyphicon-time"></span>{{ blog.created_time|date:"Y-m-d" }}
</p>
<p>{{ blog.content|truncatechars:30 }}</p>
</div>
{% empty %}
<div class="blog">
<h3>-- 暂无博客,敬请期待 --</h3>
</div>
{% endfor %}
</div>
</div>
<div class="paginator">
<ul class="pagination">
{# 上一页 #}
<li>
{% if page_of_blogs.has_previous %}
<a href="?page={{ page_of_blogs.pageprevious_page_number }}" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
</a>
{% else %}
<span aria-hidden="true">&laquo;</span>
{% endif %}
</li>
{# 全部页码 #}
{% for page_num in page_range %}
{% if page_num == page_of_blogs.number %}
<li class="active"><span>{{ page_num }}</span></li>
{% else %}
{% if page_num == '...' %}
<li><span>{{ page_num }}</span></li>
{% else %}
<li><a href="?page={{ page_num }}">{{ page_num }}</a></li>
{% endif %}
{% endif %}
{% endfor %}
{# 下一页 #}
<li>
{% if page_of_blogs.has_next %}
<a href="?page={{ page_of_blogs.next_page_number }}" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
{% else %}
<span aria-hidden="true">&raquo;</span>
{% endif %}
</li>
</ul>
<p>
共有{{ page_of_blogs.paginator.count }}篇博客,
当前第{{ page_of_blogs.number }}页,共{{ page_of_blogs.paginator.num_pages }}页
</p>
</div>
</div>
<div class="hidden-xs col-sm-4 col-md-3 col-lg-2">
<div class="panel panel-default">
<div class="panel-heading">博客分类</div>
<div class="panel-body">
<ul class="blog-types">
{% for blog_type in blog_types %}
<li><a href="{% url 'blogs_with_type' blog_type.pk %}">{{ blog_type.type_name }}</a></li>
{% empty %}
<li>暂无分类</li>
{% endfor %}
</ul>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">日期归档</div>
<div class="panel-body">
<ul>
{% for blog_date in blog_dates %}
<li>
<a href="{% url 'blogs_with_date' blog_date.year blog_date.month %}">
{{ blog_date|date:"Y年m月"}}
</a>
</li>
{% endfor %}
</ul>
</div>
</div>
</div>
</div>
</div>
{% endblock %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% extends 'blog/blog_list.html' %}
{% block title %}{{ blog_type.type_name }}{% endblock %}

{% block blog_list_title %}
日期归档:{{ blogs_with_date }}
<a href="{% url 'blog_list' %}">查看全部博客</a>
{% endblock %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% extends 'blog/blog_list.html' %}
{% block title %}{{ blog_type.type_name }}{% endblock %}

{% block blog_list_title %}
分类:{{ blog_type.type_name }}
<a href="{% url 'blog_list' %}">查看全部博客</a>
{% endblock %}
3 changes: 3 additions & 0 deletions 15.上下篇博客和按月分类/blog/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
11 changes: 11 additions & 0 deletions 15.上下篇博客和按月分类/blog/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.urls import path
from . import views

# start with blog
urlpatterns = [
# http://localhost:8000/blog/1
path('', views.blog_list, name='blog_list'),
path('<int:blog_pk>', views.blog_detail, name="blog_detail"),
path('type/<int:blog_type_pk>', views.blogs_with_type, name="blogs_with_type"),
path('date/<int:year>/<int:month>', views.blogs_with_date, name="blogs_with_date"),
]
57 changes: 57 additions & 0 deletions 15.上下篇博客和按月分类/blog/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from django.shortcuts import render_to_response, get_object_or_404
from django.core.paginator import Paginator
from django.conf import settings
from .models import Blog, BlogType

def get_blogs_list_common_data(request, blogs_all_list):
paginator = Paginator(blogs_all_list, settings.EACH_PAGE_BLOGS_NUMBER)
page_num = request.GET.get('page', 1) # 获取url的页码参数(GET请求)
page_of_blogs = paginator.get_page(page_num)
currentr_page_num = page_of_blogs.number # 获取当前页码
# 获取当前页面前后各2页的页码范围
page_range = list(range(max(currentr_page_num - 2, 1), currentr_page_num)) + \
list(range(currentr_page_num, min(currentr_page_num + 2, paginator.num_pages) + 1))
# 加上省略页码标记
if page_range[0] - 1 >= 2:
page_range.insert(0, '...')
if paginator.num_pages - page_range[-1] >= 2:
page_range.append('...')
# 加上首页和尾页
if page_range[0] != 1:
page_range.insert(0, 1)
if page_range[-1] != paginator.num_pages:
page_range.append(paginator.num_pages)

context = {}
context['blogs'] = page_of_blogs.object_list
context['page_of_blogs'] = page_of_blogs
context['page_range'] = page_range
context['blog_types'] = BlogType.objects.all()
context['blog_dates'] = Blog.objects.dates('created_time', 'month', order="DESC")
return context

def blog_list(request):
blogs_all_list = Blog.objects.all()
context = get_blogs_list_common_data(request, blogs_all_list)
return render_to_response('blog/blog_list.html', context)

def blogs_with_type(request, blog_type_pk):
blog_type = get_object_or_404(BlogType, pk=blog_type_pk)
blogs_all_list = Blog.objects.filter(blog_type=blog_type)
context = get_blogs_list_common_data(request, blogs_all_list)
context['blog_type'] = blog_type
return render_to_response('blog/blogs_with_type.html', context)

def blogs_with_date(request, year, month):
blogs_all_list = Blog.objects.filter(created_time__year=year, created_time__month=month)
context = get_blogs_list_common_data(request, blogs_all_list)
context['blogs_with_date'] = '%s年%s月' % (year, month)
return render_to_response('blog/blogs_with_date.html', context)

def blog_detail(request, blog_pk):
context = {}
blog = get_object_or_404(Blog, pk=blog_pk)
context['previous_blog'] = Blog.objects.filter(created_time__gt=blog.created_time).last()
context['next_blog'] = Blog.objects.filter(created_time__lt=blog.created_time).first()
context['blog'] = blog
return render_to_response('blog/blog_detail.html', context)
Binary file added 15.上下篇博客和按月分类/db.sqlite3
Binary file not shown.
15 changes: 15 additions & 0 deletions 15.上下篇博客和按月分类/manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
try:
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?"
) from exc
execute_from_command_line(sys.argv)
Empty file.
Loading

0 comments on commit bca3563

Please sign in to comment.