BBS项目分布搭建二
1. 首页详情补充
# 在home.html文件中 body标签内补充:
2. 模拟产生数据
# 创建后台管理员系统所需的表
在 admin.py文件中写入代码:
from django.contrib import admin
# Register your models here.
from app01 import models
# 注册数据表
admin.site.register(models.UserInfo)
admin.site.register(models.Blog)
admin.site.register(models.Tag)
admin.site.register(models.Category)
admin.site.register(models.Article)
admin.site.register(models.Article2Tag)
admin.site.register(models.UpAndDown)
admin.site.register(models.Comment)
# 登录后台管理员系统
"""由于表名都是英文的 可以自行改为中文 在每个表下面加上 class Meta 即可"""
### 在 models.py文件中修改表配置:
from django.contrib.auth.models import AbstractUser
# 1. 用户表
class UserInfo(AbstractUser):
phone = models.CharField(max_length=32, verbose_name='手机号', default='', blank=True)
'''
default='' 指定的是数据库中的默认值
blank=True:django限制的必填项
'''
# 头像
avatar = models.FileField(upload_to='static/img', default='static/img/default.png')
# 创建时间
create_time = models.DateTimeField(auto_now_add=True)
blog = models.OneToOneField(to='Blog', null=True)
class Meta:
# db_table = 'aaa'
verbose_name = '用户表'
verbose_name_plural = verbose_name
# 2. 站点表
class Blog(models.Model):
site_name = models.CharField(max_length=64, verbose_name='站点名称')
site_title = models.CharField(max_length=64, verbose_name='站点标题')
site_style = models.CharField(max_length=64, verbose_name='站点样式')
class Meta:
# verbose_name = '站点表'
verbose_name_plural = '站点表'
def __str__(self):
return self.site_name
# 3. 标签表
class Tag(models.Model):
title = models.CharField(max_length=64, verbose_name='标签名称')
def __str__(self):
return self.title
class Meta:
verbose_name_plural = '标签表'
# 4. 分类表
class Category(models.Model):
title = models.CharField(max_length=64, verbose_name='分类名称')
class Meta:
verbose_name_plural = '分类表'
def __str__(self):
return self.title
# 5. 文章表
class Article(models.Model):
title = models.CharField(max_length=128, verbose_name='文章标题')
desc = models.CharField(max_length=512, verbose_name='文章简介')
content = models.TextField(verbose_name='文章内容')
create_time = models.DateTimeField(auto_now_add=True)
# 外键关系
blog = models.ForeignKey(to='Blog')
category = models.ForeignKey(to='Category')
# 多对多
tags = models.ManyToManyField(to='Tag',
through='Article2Tag',
through_fields=('article', 'tag'))
# 优化字段
up_num = models.IntegerField(verbose_name='点赞数', default=0)
down_num = models.IntegerField(verbose_name='点踩数', default=0)
comment_num = models.IntegerField(verbose_name='评论数', default=0)
def __str__(self):
return self.title
class Article2Tag(models.Model):
article = models.ForeignKey(to='Article')
tag = models.ForeignKey(to='Tag')
# 6. 点赞点踩表
class UpAndDown(models.Model):
user = models.ForeignKey(to='UserInfo')
article = models.ForeignKey(to='Article')
# 布尔值:True/False => 数据库中显示为: 1/0
is_up = models.BooleanField()
class Meta:
verbose_name_plural = '点赞点踩表'
# 7. 评论表
class Comment(models.Model):
user = models.ForeignKey(to='UserInfo')
article = models.ForeignKey(to='Article')
content = models.CharField(max_length=512, verbose_name='评论内容')
create_time = models.DateTimeField(auto_now_add=True)
# parent_id = models.ForeignKey(to='Comment')
parent = models.ForeignKey(to='self')
class Meta:
verbose_name_plural = '评论表'
3. 首页数据展示
# 在 views.py中修改:
# 4. 首页
def home(request):
article_list = models.Article.objects.all()
return render(request, 'home.html', locals())
# 在home.html文件中修改 即可:
{% for article in article_list %}
{% endfor %}
4. 个人站点页和404页面搭建
# 添加路由:
# 个人站点页,缺点:匹配的范围太大,
url(r'^(?P\w+)/', views.blog),
# 在views.py中添加功能:
# 7.个人站点页
def blog(request, username):
'''
验证站点是否存在
:param request:
:param username:
:return:
'''
# 验证站点的存在性 404页面
user_obj = models.UserInfo.objects.filter(username=username).first()
if not user_obj:
# 应该返回定制的404页面
return render(request, '404.html')
return render(request, 'blog.html')
# 新建blog.html文件内容:
Title
# 新建404.html文件内容:
可以直接复制任意网站404页面内容拿来使用
5. 个人站点文章展示功能
# 7.个人站点页
def blog(request, username):
'''
验证站点是否存在
:param request:
:param username:
:return:
'''
# 验证站点的存在性 404页面
user_obj = models.UserInfo.objects.filter(username=username).first()
if not user_obj:
# 应该返回定制的404页面
return render(request, '404.html')
blog = user_obj.blog # 个人站点
# 查询当前站点的所有文章列表
article_list = models.Article.objects.filter(blog=blog).all()
return render(request, 'blog.html', locals())
# blog.html文件只修改 即可:
{% for article in article_list %}
{% endfor %}
6. 个人站点页 标签和分类展示
# 在models.py中 对分类表和标签表创建外键关系 之后重新迁移数据库:
blog = models.ForeignKey(to='Blog', null=True) # 外键
# 修改blog.html文件中 :
我的标签
{% for tag in tag_list %}
{% endfor %}
我的分类
{% for category in category_list %}
{% endfor %}