个人博客开发之 项目表结构设计

  

项目源码下载:http://download.vhosts.cn

 

思考:

博客主要用于文章、图片、视频等资讯内容发布, 既然是发布类站点,首先得有发布的内容,然后需要有人对其进行管理,还需要可以评论,如果后期有会员机制还需要有登录和用户中心等。所以基本可以分为两大类,一类为发布内容,一类为用户管理。

    image.png 

有了数据表结构逻辑之后,可以区分apps 模块为两个,一个users 一个 blogs

users 模块两张表:userprofile 用户表和emailverifycode用户注册表

blogs 模块4张表: artical 文章表、tags文章标签表、catagroy文章分类表和comments文章评论表

 

 

有了上述思考和归纳,就可以新建我们的users 、blogs两个app。

在项目根目录下执行如下命令:

image.png

 

新建完成后,将两个app 移动到apps 包中

 

image.png

 

 

一、创建app models

    vim apps\users\models.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.contrib.auth.models 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)
    createtime = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
     
    class Meta:
        app_label = 'users'
        verbose_name = '邮箱验证码'
        verbose_name_plural = verbose_name
        db_table = 'email_verify_recode'
    def __unicode__(self):
        return self.code

   vim apps\blogs\models.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
from DjangoUeditor.models 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='博客文章')
    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 = 'comments'
        ordering = ['-updatetime']
    def __unicode__(self):
        return self.username

 

二、配置路由(未区分项目模块,urls 分离到各个app,需要在app中新建urls.py文件)

 

    2.1 配置项目主路由

    vim cpyblog\cpyblog\urls.py

from django.conf.urls import url, include
from django.contrib import admin
import xadmin
from django.conf.urls.static import static
from django.conf 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'
from django.conf.urls import url
urlpatterns = [
]

  


    2.3 配置cpyblog\apps\blogs\urls.py    

 

#!/usr/bin/env python

# -*- coding: utf-8 -*-
 from django.conf.urls import url
 urlpatterns = [
 ]

  

posted @ 2019-02-13 10:08  池偏一  阅读(1161)  评论(0编辑  收藏  举报