使用django + KindEditor 开发个人博客系统
阅读原文时间:2023年07月09日阅读:2

1. 通过url参数组合不同的过滤条件

django框架部分

1. 数据结构models

from django.db import models

Create your models here.

大分类表

class Category(models.Model):
caption = models.CharField(max_length=32)

文章类型表

class ArticleTyp(models.Model):
caption = models.CharField(max_length=32)

创建一个文章表,设置标题和内容;文章大分类小分类 四个字段

class Article(models.Model):
title = models.CharField(max_length=1024)
content = models.CharField(max_length=30720)
category = models.ForeignKey(Category, null=True, on_delete=models.SET_NULL)
article_type = models.ForeignKey(ArticleTyp, null=True, on_delete=models.SET_NULL)
# 测试使用内存级别选项,不存储于数据库
# type_choice = (
# (1, "Python"),
# (2, "JavaScript"),
# (3, "前端"),
# )
# article_type_id = models.IntegerField(choices=type_choice)

2. url

url(r'^article/-(?P\d+)-(?P\d+).html', blog_views.Article.as_view()),

3.views

from django.shortcuts import render

from django.views import View
from Blog import models

Create your views here.

class Article(View):
def get(self, request, *args, **kwargs):
# 查询时的过滤条件
condition = {}
for k, v in kwargs.items():
# 渲染html文件中判断是否为选中分类:url参数为字符串;数据库查询出来的id为int类型
# 转换为同一个类型,渲染模板时做比较
kwargs[k] = int(v)
if v == '0': # 如果为零,查询条件中就不传入此参数
pass
else:
condition[k] = v

    print(condition)  
    article\_type\_list = models.ArticleTyp.objects.all()  
    # models 中使用内存级别字段  
    # article\_type\_list = models.Article.type\_choice  
    category\_list = models.Category.objects.all()  
    result = models.Article.objects.filter(\*\*condition)  
    return render(  
        request,  
        # 'blog/article.html',  
        'blog/article\_simple\_tag.html',  
        {'article\_type\_list': article\_type\_list,  
         'category\_list': category\_list,  
         'articles': result,  
         'condition\_dict': kwargs}  
    )

4. models 数据库类型核对

from django.db import models

Create your models here.

大分类表

class Category(models.Model):
caption = models.CharField(max_length=32)

文章类型表

class ArticleTyp(models.Model):
caption = models.CharField(max_length=32)

创建一个文章表,设置标题和内容;文章大分类小分类 四个字段

class Article(models.Model):
title = models.CharField(max_length=1024)
content = models.CharField(max_length=30720)
category = models.ForeignKey(Category, null=True, on_delete=models.SET_NULL)
article_type = models.ForeignKey(ArticleTyp, null=True, on_delete=models.SET_NULL)
# 测试使用内存级别选项,不存储于数据库
# type_choice = (
# (1, "Python"),
# (2, "JavaScript"),
# (3, "前端"),
# )
# article_type_id = models.IntegerField(choices=type_choice)

5.自定义html模板 函数 生成过滤项的a 标签html字符串

templatetags/condition_filter.py

#!/usr/bin/env python3

-*- coding:utf-8 -*-

@Time: 2020/8/8 11:17

@Author:zhangmingda

@File: condition_filter.py

@Software: PyCharm

Description:

from django import template
from django.utils.safestring import mark_safe

register = template.Library()

@register.simple_tag
def filter_all(condition_dict, all_condition):
article_type_id = condition_dict["article_type_id"]
category_id = condition_dict["category_id"]

all\_ret = ""  
if all\_condition == "article\_type\_id":  
    if article\_type\_id == 0:  
        all\_ret = '<a href="/article/-0-%s.html" class="condition active">全部</a>' % category\_id  
    else:  
        all\_ret = '<a href="/article/-0-%s.html" class="condition ">全部</a>' % category\_id  
if all\_condition == "category\_id":  
    if category\_id == 0:  
        all\_ret = '<a href="/article/-%s-0.html" class="condition active">全部</a>' % article\_type\_id  
    else:  
        all\_ret = '<a href="/article/-%s-0.html" class="condition ">全部</a>' % article\_type\_id

all\_ret = mark\_safe(all\_ret)

return all\_ret

@register.simple_tag
def filter_article_type(condition_dict, article_type_list ):
"""
{% if condition_dict.article_type_id == article_type.id %}
{{ article_type.caption }}
{% else %}
{{ article_type.caption }}
{% endif %}
:return:
"""
a_tag_list = []
for article_type in article_type_list:

    article\_type\_id = article\_type.id  
    # models 中使用内存级别字段  
    # article\_type\_id = article\_type\[0\]

    if condition\_dict\["article\_type\_id"\] == article\_type\_id:  
        ret = '<a href="/article/-{article\_type\_id}-{category\_id}.html" class="condition active">{article\_type\_caption}</a>'.format(  
            article\_type\_id=article\_type\_id,  
            category\_id=condition\_dict.get('category\_id'),  
            article\_type\_caption=article\_type.caption,  
            # models 中使用内存级别字段  
            # article\_type\_caption=article\_type\[1\]  
        )  
    else:  
        ret = '<a href="/article/-{article\_type\_id}-{category\_id}.html" class="condition ">{article\_type\_caption}</a>'.format(  
            article\_type\_id=article\_type\_id,  
            category\_id=condition\_dict.get('category\_id'),  
            article\_type\_caption=article\_type.caption,  
            # models 中使用内存级别字段  
            # article\_type\_caption=article\_type\[1\]  
        )  
    a\_tag\_list.append(ret)  
all\_a\_tag = ''.join(a\_tag\_list)  
return mark\_safe(all\_a\_tag)

@register.simple_tag
def filter_category(condition_dict, category_list ):
"""
{% for category in category_list %}
{% if condition_dict.category_id == category.id %}
{{ category.caption }}
{% else %}
{{ category.caption }}
{% endif %}
{% endfor %}
:param condition_dict:
:param category_list:
:return:
"""
filter_category_tag_list = []
for category in category_list:
category_id = category.id
if condition_dict.get("category_id") == category_id:
category_a_tag = '%s' % (condition_dict.get("article_type_id"), category_id, category.caption )
else:
category_a_tag = '%s' % (condition_dict.get("article_type_id"), category_id, category.caption )
filter_category_tag_list.append(category_a_tag)
ret = ''.join(filter_category_tag_list)
return mark_safe(ret)

7.html 加载自定义模板方法

{% load condition_filter %}

Title

过滤条件

{% filter_all condition_dict "article_type_id" %} {% filter_article_type condition_dict article_type_list %}
{% filter_all condition_dict "category_id" %} {% filter_category condition_dict category_list %}

查询结果

    {% for article in articles %}
  • {{ article.id }} - {{ article.title }}
  • {% endfor %}

数据库设计

"""

用户表:UserInfo
nid 自增列
username 用户名 不可重复
password 密码
nickname 昵称
email 邮箱
avtar 头像
create_time 创建时间
fans 粉丝们 ---- 多对多表 外键自己

个人博客信息表:Blog
nid 自增列
title 个人博客标题
site 个人博客前缀
theme 博客主题
user 博客一对一 对应用户id

互粉关系表:UserFans
user 博主 外键到用户表
folower 粉丝 外键到用户表
class Meta: 不可重复
unique_together = [('user', 'follower')]

文章分类表(博主个人):Category
nid 自增
title 标题
blog 所属博客 -- 外键到博客表

文章标签表:Tag
nid 自增主键
title 标签名称
blog 所属博客 - 外键到博客表-主键

文章内容表:ArticleDetail
content 文章内容
aritcle 所属文章 - 外键到文章信息表

顶/踩 统计:UpDown
aritcle 对应文章 - 外键到 文章表的主键
user 用户 外键到用户表主键
up 顶或者是踩:布尔值即可
class Meta: # 两个字段合并唯一 :顶或者踩,一篇文章只能顶或者踩一次
unique_together = [('article', 'user')]

评论表:Comment
nid 自增主键
content 内容
create_time 创建时间
reply 回复/评论 对自己表中已有的数据:self
article 被评论的文章 - 外键到文章表主键
user 评论着 - 外键到用户表 主键

文章表:Article
nid 自增主键
title 文章标题
summary 文章简介
read_count 阅读量
comment_count 评论数量
up_count 点赞数量
down_count 踩量
create_time 创建时间
blog 所属博客
category 文章类型
type_choices 语言分类选项
article_type 语言分类
tags 标签 多对多 指定多对多的第三张表名Article2Tag

文章-标签多对多表:Article2Tag
article 文章 外键到文章表主键
tag 标签 外键到标签表主键
class Meta: 标签&文章一起,不得重复
unique_together = [('article', 'tag')]

"""

数据库表结构代码

models.py

from django.db import models

Create your models here.

class UserInfo(models.Model):
"""
用户表
"""
uid = models.BigAutoField(primary_key=True)
username = models.CharField(max_length=32, unique=True, verbose_name="用户名")
password = models.CharField(verbose_name='密码', max_length=64)
nickname = models.CharField(verbose_name='昵称', max_length=32)
email = models.EmailField(verbose_name='邮箱', unique=True)
avatar = models.ImageField(verbose_name='头像')
create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
fans = models.ManyToManyField(verbose_name='粉丝们',
to='UserInfo',
through='UserFans',
through_fields=('user', 'follower'))

class UserFans(models.Model):
"""互粉关系表"""
user = models.ForeignKey(verbose_name='博主', to=UserInfo, to_field='uid', related_name='users', on_delete=models.SET_NULL, null=True)
follower = models.ForeignKey(verbose_name='粉丝', to=UserInfo, to_field='uid', related_name='followers', on_delete=models.SET_NULL, null=True)
class Meta:
unique_together = [('user', 'follower')]

class Blog(models.Model):
"""
博客信息
"""
bid = models.BigIntegerField(primary_key=True)
title = models.CharField(verbose_name='标题', max_length=128)
site = models.CharField(verbose_name="个人博客前缀", max_length=32, unique=True)
theme = models.CharField(verbose_name='博客主题', max_length=32)
user = models.OneToOneField(to='UserInfo', to_field='uid', on_delete=models.SET_NULL, null=True)

class Category(models.Model):
"""博主个人文章分类"""
cid = models.AutoField(primary_key=True)
title = models.CharField(verbose_name='分类标题', max_length=32)
blog = models.ForeignKey(verbose_name='所属博客', to='Blog', to_field='bid',
on_delete=models.SET_NULL,null=True)

class Tag(models.Model):
tid = models.AutoField(primary_key=True)
title = models.CharField(verbose_name='标签名称',max_length=32)
blog = models.ForeignKey(verbose_name="所属文章",to='Blog', to_field='bid',on_delete=models.SET_NULL,
null=True)

class Article(models.Model):
"""文章信息表"""
aid = models.BigAutoField(primary_key=True)
title = models.CharField(verbose_name="文章标题", max_length=128)
summary = models.CharField(verbose_name="文章简介", max_length=255)
read_count = models.IntegerField(default=0)
coumment_count = models.IntegerField(default=0)
up_count = models.IntegerField(default=0)
down_count = models.IntegerField(default=0)
create_time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)
blog = models.ForeignKey(verbose_name="所属博客", to='Blog', to_field='bid',
on_delete=models.SET_NULL, null=True)
category = models.ForeignKey(verbose_name='文章类型', to='Category',to_field='cid',
on_delete=models.SET_NULL, null=True)
type_choices = [
(1, "Python"),
(2, "Linux"),
(3, "OpenStack"),
(4, "GoLang"),
]
article_type = models.IntegerField(choices=type_choices, default=None)
tags = models.ManyToManyField(
to='Tag', through='ArticleToTag',
through_fields=('article', 'tag')
)

class ArticleToTag(models.Model):
article = models.ForeignKey(verbose_name='文章', to='Article', to_field='aid',
on_delete=models.SET_NULL, null=True)

tag = models.ForeignKey(verbose\_name="标签", to='Tag', to\_field='tid',  
                        on\_delete=models.SET\_NULL,null=True)  
class Meta:  
    unique\_together = \[  
        ('article', 'tag')  
    \]

class ArticleDetail(models.Model):
"""文章详情表"""
content = models.TextField(verbose_name="文章内容")
article = models.OneToOneField(verbose_name="所属文章", to='Article',to_field='aid',
on_delete=models.CASCADE)

class UpDown(models.Model):
"""文章顶/踩"""
article = models.ForeignKey(verbose_name="文章", to='Article', to_field='aid',
on_delete=models.CASCADE)
user = models.ForeignKey(verbose_name='顶踩用户', to='UserInfo', to_field='uid',
on_delete=models.CASCADE)
up = models.BooleanField(verbose_name='是/否赞')

class Meta:  
    """联合唯一"""  
    unique\_together = \[('article', 'user')\]

class Comment(models.Model):
"""评论表"""
nid = models.BigAutoField(primary_key=True)
content = models.CharField(verbose_name='评论内容', max_length=255)
create_time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)

reply = models.ForeignKey(verbose\_name="回复已有评论",to="self",  
                          null=True, on\_delete=models.SET\_NULL)  
article = models.ForeignKey(verbose\_name="对应的文章",  
                            to="Article",  
                            to\_field='aid',  
                            on\_delete=models.SET\_NULL,  
                            null=True)

创建数据库:python manage.py makemigrations; python manage.py migrate

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章