Django blog project [1]

Create project

Insert picture description here

Reference for each document of the project

Create application

Enter in the command line window

python manage.py startapp users
python manage.py startapp blogs
Insert picture description here
Insert picture description here

Two more project folders indicate successful creation

Insert picture description here

. Add the application name in INSTALLEN_APPS in blog/settings.py:

Insert picture description here

Write models

users-models

Because AbstractUser
is used , add a line to the configuration file (blog/settings.py)

AUTH_USER_MODEL = "users.BlogUser"
from django.contrib.auth.models import AbstractUser
from django.db import models

# Create your models here.
# 继承用户模型类并自定义用户模型
class BlogUser(AbstractUser):
    nikename=models.CharField('昵称',max_length=20,default='')

#邮箱验证数据模型
class EmailVerifyRecord(models.Model):
    code=models.CharField(max_length=20,verbose_name='验证码')
    email=models.EmailField(max_length=50,verbose_name='邮箱')
    send_type=models.CharField(verbose_name='验证码类型',
                               choices=(('register','注册'),
                                        ('forget','忘记密码')
                                        ('update_eamil','修改邮箱')),
                               max_length=30)

    # choices属性值为一个可迭代对象,如列表或者元组,迭代对象的每个成员包括两个元素。
    # 当字段设置choices属性时,字段在网页中将会以下拉列表的形式显示。
    # 列表或元组的第一个值将作为字段值保存到数据库中,第二个值用于提高字段的可读性。

    # 模型元属性
    class Mete:
        # 字段的说明 便于人读取的模型名称
        # 如果没有指定verbose_name,
        # Django会根据模型类名自动创建verbose_name,
        verbose_name='邮箱验证码'
        # 复数形式
        verbose_name_plural='邮箱验证码'
        
    # 设置返回的字段名
    def __str__(self):
        return '{0}({1})'.format(self.code,self.email)


blogs-models

from django.db import models

# Create your models here.
from users.models import BlogUser

class Banner(models.Model):
    title=models.CharField('标题',max_length=50)
    cover=models.ImageField('轮播图',upload_to='static/image/banner')
    link_url=models.URLField('图片链接',max_length=100)
    idx=models.IntegerField('索引')
    is_active=models.BooleanField('是否激活',default=False)

    class Meta:
        verbode_name='轮播图'
        verbode_name_plural='轮播图'

    def __str__(self):
        return self.title


class BlogCategory(models.Model):
    name=models.CharField('分类名称',max_length=20,default='')

    class Meta:
        verbose_name='博客分类'
        verbode_name_plural='博客分类'

    def __str__(self):
        return self.name


class Tags(models.Model):
    name = models.CharField('标签名称', max_length=20, default='')

    class Meta:
        verbose_name = '标签'
        verbose_name_plural = '标签'

    def __str__(self):
        return self.name


class Post(models.Model):
    user=models.ForeignKey(BlogUser,verbose_name='作者',on_delete=models.CASCADE)
    category=models.ForeignKey(BlogCategory,verbose_name='博客分类',default=None,on_delete=models.CASCADE)
    tags=models.ManyToManyField(Tags,verbose_name='标签')
    title=models.CharField('标题',max_length=50)
    content=models.TextField('内容')
    pub_date=models.DateTimeField('发布日期')
    cover=models.ImageField('博客封面',upload_to='static/images/post',default=None)
    views=models.IntegerField('浏览数',default=0)
    recommend=models.BooleanField('推荐博客',default=False)

    class Mate:
        verbose_name='博客'
        verbose_name_plural='博客'

    def __str__(self):
        return self.title


class Comment(models.Model):
    post=models.ForeignKey(Post,verbose_name='博客',on_delete=models.CASCADE)
    user=models.ForeignKey(BlogUser,verbose_name='作者',on_delete=models.CASCADE)
    pub_date=models.DateTimeField('发布时间')
    content=models.TextField('内容')

    class Mate:
        verbose_name='评论'
        verbose_name_plural='评论'

    def __str__(self):
        return self.content


class FriendlyLink(models.Model):
    title=models.CharField('标题',max_length=50)
    link=models.URLField('链接',max_length=100,default=None)

    class Meta:
        verbose_name='友情链接'
        verbose_name_plural='友情链接'

    def __str__(self):
        return self.title

Configuration database (MySQL)

The
specific parameters of DATABASES in blog/settings.py correspond to your own database

Insert picture description here

Database migration

python manage.py check
python manage.py makemigrations
python manage.py migrate

After execution, the corresponding data table appears in the database.

Insert picture description here


Reference materials: https://edu.csdn.net/course/detail/6680