个⼈博客开发之项⽬表结构设计
思考:
博客主要⽤于⽂章、图⽚、视频等资讯内容发布, 既然是发布类站点,⾸先得有发布的内容,然后需要有⼈对其进⾏管理,还需要可以评论,如果后期有会员机制还需要有登录和⽤户中⼼等。所以基本可以分为两⼤类,⼀类为发布内容,⼀类为⽤户管理。
有了数据表结构逻辑之后,可以区分apps 模块为两个,⼀个users ⼀个 blogs
users 模块两张表:userprofile ⽤户表和emailverifycode⽤户注册表
blogs 模块4张表: artical ⽂章表、tags⽂章标签表、catagroy⽂章分类表和comments⽂章评论表
有了上述思考和归纳,就可以新建我们的users 、blogs两个app。
在项⽬根⽬录下执⾏如下命令:
新建完成后,将两个app 移动到apps 包中
⼀、创建app models
vim apps\users\models.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
dels import AbstractUser
from django.db import models
# Create your models here.
class UserProfile(AbstractUser):
'''
⽤户表
:param id ⾃动⽣成
username 和 password 都继承AbstractUser表
blank = True 针对表单提交,提交时可以为空
auto_now 不管是创建还是修改都会更新该字段
auto_now_add 只有在创建时会更新字段,修改时保持不变
'''
USERNAME_FIELD = "username"
GENDER_CHOISE = (('M', u'男'), ('F', '⼥'))
gender = models.CharField(verbose_name='性别', choices=GENDER_CHOISE, max_length=1, default='M')
mobile = models.CharField(verbose_name='⼿机号码', max_length=11, null=True, blank=True)
photo = models.ImageField(verbose_name='头像', upload_to='images', max_length=100, default='images/default.jpg') updatetime = models.DateTimeField(verbose_name='更新时间', auto_now=True)
createtime = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
class Meta:
app_label = 'users'
verbose_name = u'⽤户'
养老保险可以补交吗verbose_name_plural = verbose_name
db_table = 'user_profile'
ordering = ['-updatetime']
def __unicode__(self):
return self.username
class EmailVerifyRecode(models.Model):
盐水浮鸡蛋的原理简单TYPE_CHOICES = (
('register', u'注册'),
('forget', u'回密码')
)
code = models.CharField(verbose_name='验证码', max_length=20)
email = models.EmailField(verbose_name='邮箱', max_length=50)
sendtype = models.CharField(verbose_name='验证码类型', max_length=20, choices=TYPE_CHOICES)
updatetime = models.DateTimeField(verbose_name='更新时间', auto_now=True)
class Meta:
app_label = 'users'
verbose_name = '邮箱验证码'
verbose_name_plural = verbose_name
db_table = 'email_verify_recode'
def __unicode__(self):
de
vim apps\blogs\models.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
dels import UEditorField
# Create your models here.
class Catagroy(models.Model):
'''
⽂章分类
'''
name = models.CharField(verbose_name='分类名', max_length=30)
parent = models.CharField(verbose_name='归属于', max_length=30, null=True, blank=True)
updatetime = models.DateTimeField(verbose_name='修改时间', auto_now=True)
开机画面修改createtime = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
class Meta:
verbose_name = u'⽂章分类'
verbose_name_plural = verbose_name
db_table = 'catagory'
ordering = ['createtime']
def __unicode__(self):
return '{} | {}'.format(self.name, self.parent)
class Tags(models.Model):
'''
⽂章标签
'''
name = models.CharField(verbose_name='标签名', max_length=30)初九祝福语图片
updatetime = models.DateTimeField(verbose_name='修改时间', auto_now=True)
createtime = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
class Meta:
verbose_name = u'⽂章标签'
verbose_name_plural = verbose_name
db_table = 'tags'
ordering = ['-updatetime']
def __unicode__(self):
return self.name
class Artical(models.Model):
'''
⽂章表
:param
'''
title = models.CharField(verbose_name='标题', max_length=50)
author = models.CharField(verbose_name='作者', max_length=50)
content = UEditorField(verbose_name=u'⽂章正⽂', width=850, height=500, toolbars="full", imagePath="images/", filePath="files/",
upload_settings={"imageMaxSize": 51200000, "videoPath": "video/"},
settings={}, command=None, blank=True)
京包里脊是什么地方的菜click_num = models.IntegerField(verbose_name='点击率', default=0)
catagroy = models.ForeignKey(Catagroy)
tags = models.ManyToManyField(Tags)
updatetime = models.DateTimeField(verbose_name='修改时间', auto_now=True)
高考加油的祝词createtime = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
class Meta:
verbose_name = u'⽂章'
verbose_name_plural = verbose_name
db_table = 'artical'
ordering = ['createtime']
def __unicode__(self):
return '{} | {}'.format(self.title, self.catagroy)
class Comments(models.Model):
'''
评论
'''
username = models.CharField(verbose_name='昵称', max_length=16, null=True, blank=True)
email = models.EmailField(verbose_name='邮箱')
content = models.CharField(verbose_name='评论内容', max_length=500)
artical = models.ForeignKey(Artical, verbose_name='博客⽂章')
createtime = models.DateTimeField(verbose_name='发布时间', auto_now_add=True)
class Meta:
verbose_name = u'⽂章评论'
verbose_name_plural = verbose_name
db_table = 'comments'
ordering = ['-updatetime']
def __unicode__(self):
return self.username
⼆、配置路由(未区分项⽬模块,urls 分离到各个app,需要在app中新建urls.py⽂件)
2.1 配置项⽬主路由
vim cpyblog\cpyblog\urls.py
f.urls import url, include
ib import admin
import xadmin
f.urls.static import static
f import settings
from users import urls as userurls
from blogs import urls as blogurls
from blogs.views import IndexView
urlpatterns = [
url(r'^$', IndexView.as_view(), name='index'),
url(r'^admin/', admin.site.urls),
url(r'^xadmin/', xadmin.site.urls),
url(r'^ueditor/', include('DjangoUeditor.urls')),
url(r'^users/', include(userurls)),
url(r'^blogs/', include(blogurls)),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
2.2 配置users app 路由
vim cpyblog\apps\users\urls.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'cpy'
f.urls import url
urlpatterns = [
]
2.3 配置cpyblog\apps\blogs\urls.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
f.urls import url
urlpatterns =[
]
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论