Skip to content

Commit

Permalink
创建git项目和上传第17节的代码
Browse files Browse the repository at this point in the history
  • Loading branch information
HaddyYang committed Feb 22, 2018
0 parents commit 8f5c1ed
Show file tree
Hide file tree
Showing 42 changed files with 1,033 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__
Empty file.
10 changes: 10 additions & 0 deletions 17.博客后台富文本编辑/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', 'readed_num', 'created_time', 'last_updated_time')
5 changes: 5 additions & 0 deletions 17.博客后台富文本编辑/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 17.博客后台富文本编辑/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 2017-12-24 17:50

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.0 on 2018-01-19 18:10

from django.db import migrations


class Migration(migrations.Migration):

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

operations = [
migrations.AlterModelOptions(
name='blog',
options={'ordering': ['-created_time']},
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 2.0 on 2018-02-04 17:47

import ckeditor.fields
from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('blog', '0002_auto_20180120_0210'),
]

operations = [
migrations.AlterField(
model_name='blog',
name='content',
field=ckeditor.fields.RichTextField(),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 2.0 on 2018-02-04 18:05

import ckeditor_uploader.fields
from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('blog', '0003_auto_20180205_0147'),
]

operations = [
migrations.AlterField(
model_name='blog',
name='content',
field=ckeditor_uploader.fields.RichTextUploadingField(),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.0 on 2018-02-07 16:59

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('blog', '0004_auto_20180205_0205'),
]

operations = [
migrations.AddField(
model_name='blog',
name='readed_num',
field=models.IntegerField(default=0),
),
]
Empty file.
24 changes: 24 additions & 0 deletions 17.博客后台富文本编辑/blog/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from django.db import models
from django.contrib.auth.models import User
from ckeditor_uploader.fields import RichTextUploadingField

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 = RichTextUploadingField()
author = models.ForeignKey(User, on_delete=models.DO_NOTHING)
readed_num = models.IntegerField(default=0)
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 17.博客后台富文本编辑/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-bottom: 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: 1em;
}
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,43 @@
{% 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>
<li>阅读({{ blog.readed_num }})</li>
</ul>
<div class="blog-content">{{ blog.content|safe }}</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 %}

110 changes: 110 additions & 0 deletions 17.博客后台富文本编辑/blog/templates/blog/blog_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
{% 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>&nbsp;&nbsp;
<span class="glyphicon glyphicon-time"></span> {{ blog.created_time|date:"Y-m-d" }}&nbsp;&nbsp;
阅读({{ blog.readed_num }})
</p>
<p>{{ blog.content|striptags|truncatechars:120 }}</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.previous_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 }} ({{ blog_type.blog_count }})
</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, blog_count in blog_dates.items %}
<li>
<a href="{% url 'blogs_with_date' blog_date.year blog_date.month %}">
{{ blog_date|date:"Y年m月" }} ({{ blog_count }})
</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 17.博客后台富文本编辑/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 17.博客后台富文本编辑/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/
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"),
]
Loading

0 comments on commit 8f5c1ed

Please sign in to comment.