Django 搭建博客网站-task03:Models的编写

Posted Abro.

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django 搭建博客网站-task03:Models的编写相关的知识,希望对你有一定的参考价值。

Django 搭建博客网站



前言

在vscode上连接虚拟机,然后输入命令

blog APP

python manage.py startapp blog # 这个之前已经创建过了,因此不需要重复创建

blog_config App

python manage.py startapp blog_config

comment app

python manage.py startapp comments

一、Models 的编写

models.py:

from django.db import models
from django.contrib.auth.models import User

# Create your models here.
class Category():
    """分类"""

    STATUS_ITEMS = (
        (1,"正常"),
        (0, "删除"),
    )
    name = models.CharField(max_length=50, verbose_name="名称")
    status = models.IntegerField(choices=STATUS_ITEMS, default=1, verbose_name="状态")
    owner = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="作者")
    is_nav = models.BooleanField(default=False, verbose_name="是否为导航")
    created_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = verbose_name_plural ='分类'

class Tag():
    """标签"""
    STATUS_ITEMS = (
        (1,"正常"),
        (0, "删除"),
    )
    name = models.CharField(max_length=50)
    owner = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="作者")
    status = models.IntegerField(choices=STATUS_ITEMS, default=1, verbose_name="状态")
    created_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = verbose_name_plural ='标签'

class Post():
    """文章"""
    STATUS_ITEMS = (
        (0, "删除"),
        (1, "正常"),
        (2, "草稿")
        
    )
    title = models.CharField(max_length=255, verbose_name="标题")
    owner = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="作者")
    category = models.ForeignKey(Category, verbose_name="分类", on_delete=models.CASCADE)
    tags = models.ManyToManyField(Tag, verbose_name="标签")
    desc = models.CharField(max_length=1024, verbose_name="摘要")
    content = models.TextField(verbose_name="正文")
    status = models.IntegerField(choices=STATUS_ITEMS, default=2, verbose_name="状态")
    created_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    def __str__(self):
        return self.title

    class Meta:
        verbose_name = verbose_name_plural ='文章'

admin.py:

from django.contrib import admin

from .models import Category, Tag, Post
# Register your models here.

@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
    pass

@admin.register(Tag)
class TagAdmin(admin.ModelAdmin):
    pass

@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
    pass
    


二、Blog_config

models.py

from django.db import models
from django.contrib.auth.models import User

# Create your models here.
class Link():
    """友情链接"""
    STATUS_ITEMS = (
        (1,"正常"),
        (0, "删除"),
    )
    title = models.CharField(max_length=50, verbose_name="网站名称")
    href = models.URLField(verbose_name="链接")
    status = models.IntegerField(choices=STATUS_ITEMS, default=1, verbose_name="状态")
    owner = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="作者")
    created_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    weight = models.IntegerField(choices=zip(range(1,11), range(1,11)),default=1, verbose_name="权重")

    def __str__(self):
        return self.title

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

class SideBar():
    """侧边栏"""
    TYPE_ITEMS = (
        (1,"html"),
        (2, "最新文章"),
        (3, "最热文章"),
        (4, "最近评论"),
    )
    STATUS_ITEMS = (
        (1,"展示"),
        (0, "隐藏"),
    )
    title = models.CharField(max_length=50, verbose_name="标题")
    display_type = models.IntegerField(choices=TYPE_ITEMS, default=1, verbose_name="展示类型")
    status = models.IntegerField(choices=STATUS_ITEMS, default=1, verbose_name="状态")
    content = models.CharField(max_length=500, verbose_name="内容")
    owner = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="作者")
    created_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    def __str__(self):
        return self.title

    class Meta:
        verbose_name = verbose_name_plural ='侧边栏'

admin.py:

from django.contrib import admin

from .models import Link,SideBar
# Register your models here.

@admin.register(Link)
class LinkAdmin(admin.ModelAdmin):
    pass

@admin.register(SideBar)
class SideBarAdmin(admin.ModelAdmin):
    pass


三、comment

models.py:

from django.db import models
from blog.models import Post
# Create your models here.

class Comment(models.Model):
    """评论"""
    target = models.ForeignKey(Post, verbose_name="评论目标", on_delete=models.CASCADE)
    nickname = models.CharField(max_length=50, verbose_name="昵称")
    email = models.EmailField(verbose_name="邮箱")
    website = models.URLField(verbose_name="网站地址")
    content = models.TextField(verbose_name="评论内容")
    created_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    def __str__(self):
        return f"{self.target} By {self.nickname}"

    class Meta:
        verbose_name = verbose_name_plural ='评论'

admin.py:

from django.contrib import admin
from .models import Comment
# Register your models here.
@admin.register(Comment)
class CommentAdmin(admin.ModelAdmin):
    pass

这些就是后面网站要用到的数据字段。


总结

近期要期末考试了,未能写得更详细点,后续会更新文章呜呜呜;-),抱歉了各位老铁汁们!

以上是关于Django 搭建博客网站-task03:Models的编写的主要内容,如果未能解决你的问题,请参考以下文章

Django 搭建博客网站-task02:快速搭建网站

Django 搭建博客网站-task01:基础知识

Django搭建博客网站

Django搭建博客网站

Django搭建博客网站

Django搭建博客网站