博客园
1 设计表关系
登陆与注册 :User blog
系统首页: article article_detail
个人站点: 标签表,分类表
文章详细页:文章点赞踩灭表 评论表
from django.db import models # Create your models here. from django.contrib.auth.models import AbstractUser class UserInfo(AbstractUser): """ 用户信息 """ nid = models.AutoField(primary_key=True) telephone = models.CharField(max_length=11, null=True, unique=True) avatar = models.FileField(upload_to='avatars/', default="/avatars/default.png") create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True) blog = models.OneToOneField(to='Blog', to_field='nid', null=True) def __str__(self): return self.username class Blog(models.Model): """ 博客(个人站点)信息 """ nid = models.AutoField(primary_key=True) title = models.CharField(verbose_name='个人博客标题', max_length=64) site = models.CharField(verbose_name='个人博客后缀', max_length=32, unique=True) theme = models.CharField(verbose_name='博客主题', max_length=32) def __str__(self): return self.title class Category(models.Model): """ 博主个人文章分类表 """ nid = models.AutoField(primary_key=True) title = models.CharField(verbose_name='分类标题', max_length=32) blog = models.ForeignKey(verbose_name='所属博客', to='Blog', to_field='nid') def __str__(self): return self.title class Tag(models.Model): nid = models.AutoField(primary_key=True) title = models.CharField(verbose_name='标签名称', max_length=32) blog = models.ForeignKey(verbose_name='所属博客', to='Blog', to_field='nid') def __str__(self): return self.title class Article(models.Model): nid = models.AutoField(primary_key=True) title = models.CharField(max_length=50, verbose_name='文章标题') desc = models.CharField(max_length=255, verbose_name='文章描述') create_time = models.DateTimeField(verbose_name='创建时间') # comment_count=models.IntegerField(default=0) # up_count=models.IntegerField(default=0) # down_count=models.IntegerField(default=0) category = models.ForeignKey(to='Category', to_field='nid', null=True) user = models.ForeignKey(verbose_name='作者', to='UserInfo', to_field='nid') tags = models.ManyToManyField( to="Tag", through='Article2Tag', through_fields=('article', 'tag'), ) def __str__(self): return self.title class ArticleDetail(models.Model): """ 文章详细表 """ nid = models.AutoField(primary_key=True) content = models.TextField() article = models.OneToOneField(to='Article', to_field='nid') class Article2Tag(models.Model): nid = models.AutoField(primary_key=True) article = models.ForeignKey(verbose_name='文章', to="Article", to_field='nid') tag = models.ForeignKey(verbose_name='标签', to="Tag", to_field='nid') class Meta: unique_together = [ ('article', 'tag'), ] def __str__(self): v = self.article.title + "--" + self.tag.title return v class ArticleUpDown(models.Model): """ 点赞表 """ nid = models.AutoField(primary_key=True) user = models.ForeignKey('UserInfo', null=True) article = models.ForeignKey("Article", null=True) is_up = models.BooleanField(default=True) class Meta: unique_together = [ ('article', 'user'), ] class Comment(models.Model): """ 评论表 """ nid = models.AutoField(primary_key=True) article = models.ForeignKey(verbose_name='评论文章', to='Article', to_field='nid') user = models.ForeignKey(verbose_name='评论者', to='UserInfo', to_field='nid') content = models.CharField(verbose_name='评论内容', max_length=255) create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True) parent_comment = models.ForeignKey('self', null=True) def __str__(self): return self.content
思考
由于UserInfo(AbstractUser) 不同于其他类的继承关系,migrate的时候可能会报错。
解决办法:编辑[项目]/settings,添加
AUTH_USER_MODEL="blog.UserInfo"
2 基于ajax和认证组件实现的登陆验证
3 基于ajax和forms组件实现的注册功能
---- forms组件钩子
---- 上传文件
4 系统首页
5 个人站点页面(ORM)
6 文章详细页的设计
7 点赞与评论
8 文本编辑器与bs模块防止xss攻击(对于文章的增删改查)
content-Type:
if POST:
forms
name="alex"
age=122
ajax
data:{
name="alex"
age=122
}
"GET http/1.1 url?
请求头
请求体"
if urlencoded:
"GET http/1.1 url?
请求头
name=alex&age=122"
if json:
"GET http/1.1 url?
请求头
'{"name":"alex","age":122}'"
if formdata:
在ajax:
$.ajax({
url:"",
type:"post",
contentType:false,
processData:false,
data:formdata,
success:function (data) {
console.log(data)
}
})
今日作业:
1 加入钩子验证: 1.1 校验用户名是否存在 1.2 校验两次密码是否一致 2 校验成功,添加记录
今日总结
上节回顾
番外
1.ForeignKey(Verbose_name)
2.查看源码:
from django.conf import settings
from django.conf import global_settings
is_valid()源码