Django博客重构教程(一)models模型设计

更新时间:2023-06-09 16:02:43 阅读: 评论:0

Django博客重构教程(⼀)models模型设计这⾥主要是三张表Tag、Category、Post,关于评论,后⾯再加
blogproject/models
安装包
pip install django-model-utils
pip install pillow
pip install django-imagekit
pip install django-uuslug
pip install markdown刺五加作用
最忆是杭州from django.db import models
dels import Ur
from dels import F
from django.urls import rever
from django.utils import timezone
from django.utils.functional import cached_property
from django.utils.html import strip_tags
from anslation import gettext_lazy as _
from model_utils import Choices
from dels import TimeStampedModel
dels import ImageSpecField
# Create your models here.
from uuslug import slugify
from blogproject.utils import generate_rich_content
雪里红炒肉
class Category(TimeStampedModel):
id = models.AutoField(primary_key=True)
name = models.CharField(_("name"), max_length=100)
slug = models.SlugField(_("slug"), editable=Fal, max_length=200, unique=True)
def natural_key(lf):
return lf.__str__()
def save(lf, *args, **kwargs):
lf.slug = slugify(lf.name)
super(Category, lf).save(*args, **kwargs)
def __str__(lf):
return lf.name
三星重工
class Tag(TimeStampedModel):
id = models.AutoField(primary_key=True)
name = models.CharField(_("name"), max_length=100)
class Meta:
verbo_name = _("tag")
verbo_name_plural = _("tags")
def natural_key(lf):
return lf.__str__()
def __str__(lf):
def __str__(lf):
return lf.name
class Post(models.Model):
STATUS_CHOICES = Choices(
(1, "published", _("published")),
(2, "draft", _("draft")),
(3, "hidden", _("hidden")),
)
author = models.ForeignKey(Ur, on_delete=models.CASCADE)
title = models.CharField(max_length=100)
content = models.TextField()
excerpt = models.TextField(_("excerpt"), blank=True)
views = models.PositiveIntegerField(_("views"), default=0, editable=Fal)
created_time = models.DateTimeField(_("created time"), w)    updated_time = models.DateTimeField(_("created time"), auto_now=True)
likes = models.PositiveIntegerField(default=0)
url_slug = models.SlugField(editable=Fal, max_length=200)
status = models.PositiveSmallIntegerField(
_("status"), choices=STATUS_CHOICES, default=STATUS_CHOICES.draft    )
tags = models.ManyToManyField(Tag, verbo_name=_("tags"), blank=True)
category = models.ForeignKey(
Category,
on_delete=models.CASCADE,
verbo_name=_("category"),
null=True,
blank=True,
)
class Meta:
verbo_name = _("Posts")
verbo_name_plural = _("Posts")
# ordering = ('-created',)
def __str__(lf):
return lf.title
def get_absolute_url(lf):
return rever('post:article_detail', args=[lf.id])
def save(lf, *args, **kwargs):
lf.url_slug = slugify(lf.title)
super(Post, lf).save(*args, **kwargs)
if pt:
if ated_time and lf.status == lf.STATUS_CHOICES.published:            lf.created_time = lf.created_time
def increa_views(lf):
lf.__class__.objects.filter(id=lf.id).update(views=F("views") + 1)
@property
def body_html(lf):
return lf.("content", "")
@cached_property
def rich_content(lf):
return generate_rich_t)
@property
@property
def type(lf):
return "p"
⽤到⼏个⾃⼰写的模块
blogproject/utils
import re
import markdown
from bs4 import BeautifulSoup
import TocExtension
from slugify import slugify
def generate_rich_content(value, *, toc_depth=2, toc_url=""):
md = markdown.Markdown(
extensions=[
"a",
"dehilite",
"sions.admonition",
"sions.nl2br",
TocExtension(slugify=slugify, toc_depth=toc_depth),
"pymdownx.magiclink",
"pymdownx.tasklist",
"pymdownx.tilde",
"pymdownx.caret",
]
)
content = md.convert(value)
toc = md.toc
防煤气中毒soup = BeautifulSoup(toc, "html.parr")
# must u contents, not children
# if ts:
toc = "".join(map(str, ts)).strip()
if toc_url:
def absolutify(matchobj):
return 'href="{toc_url}{frag}"'.format(
初中语文课文
toc_url=toc_url, up(1)
)
toc = re.sub('href="(.+)"', absolutify, toc)
return {"content": content, "toc": toc}英伦学院
blogproject/urls
f.urls import url, include
from django.urls import path
from blogproject import views
身上长红痣
from rest_framework import routers
from django.views.decorators.csrf import csrf_exempt
app_name = 'blogproject'
urlpatterns = [
url(r'^post/(?P<article_id>[0-9]+)/(?P<url_slug>[-\w]+)/$', views.post_detail, name='post'), ]
模型⽣成和迁移
python3 manage.py makemigrations
python3 manage.py migrate
这⾥如果发⽣问题的话,把app下migrations⾥的initial.py和所有迁移记录的py⽂件都删掉,然后在django-migrations⾥把对应的初始化initial操作记录删掉,然后重新执⾏makemigrations和migrate

本文发布于:2023-06-09 16:02:43,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1030856.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:操作   记录   模型   迁移   重构   英伦   教程   学院
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图