1.Models设计:
1.重构用户表:
1.在users/models.py中:
from django.db import models from django.contrib.auth.models import AbstractUser from datetime import datetime # Create your models here. class UserProfile(AbstractUser): """ 用户表 """ token = models.CharField(max_length=64, null=True, blank=True, verbose_name='token') mobile = models.CharField(max_length=11, null=True, blank=True,verbose_name='电话') class Meta: verbose_name='用户' verbose_name_plural = verbose_name def __str__(self): return self.username
2.在settings.py中配置
AUTH_USER_MODEL='users.UserProfile'
3.在终端执行数据更新命令,生成数据表
由于重构了用户数据表,所以需要先注释掉settings.py中的:
'django.contrib.admin',
然后执行数据更新命令:
python manage.py makemigrations python manage.py migrate
然后再去掉注释
如果没有这一步,直接执行数据更新命令,则会报错:
django.db.migrations.exceptions.InconsistentMigrationHistory: Migration admin.0001_initial is applie
2.产品项目案例表
1.安装DjangoUeditor
软件的信息中使用到了富文本编辑框,所以要储存富文本文件。
1.将DjangoUeditor包放到extra_apps目录下
2.在settings.py中注册DjangoUeditor
3.在newnet/urls.py中配置富文本路由:
from django.contrib import admin from django.urls import path,include from django.views.static import serve from newnet.settings import MEDIA_ROOT from django.views.generic import TemplateView urlpatterns = [ path('admin/', admin.site.urls), path('media/<path:path>',serve,{'document_root':MEDIA_ROOT}), path('', TemplateView.as_view(template_name='index.html'), name='index'), path('good/', include('good.urls',namespace='goods')), path('ueditor/',include('DjangoUeditor.urls' )) ]
2.apps/good/models.py:
from django.db import models from datetime import datetime from DjangoUeditor.models import UEditorField from users.models import UserProfile from django.utils.safestring import mark_safe # Create your models here. class Case(models.Model): """ 项目 """ user = models.ForeignKey(UserProfile, verbose_name="编辑者", on_delete=models.CASCADE) img = models.ImageField(upload_to='img', null=True,verbose_name='封面') name = models.CharField(default="", max_length=30, verbose_name="项目名", help_text="项目名") type = models.CharField(default="普通项目", max_length=10, verbose_name="项目类型", help_text="项目类型") goods_desc = UEditorField(verbose_name=u"内容", imagePath="goods/images/", width=1000, height=900, filePath="goods/files/", default='') add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间") class Meta: verbose_name = "项目案例" verbose_name_plural = verbose_name def __str__(self): return self.name def image_url(self): return mark_safe('<img src="/media/{0}" class="field_img">'.format(self.img)) image_url.short_description = '封面' class News(models.Model): """ 新闻 """ is_banner=models.BooleanField(default=False,verbose_name='是否轮播') banner=models.ImageField(upload_to='banner', null=True,blank=True,verbose_name='轮播图') user = models.ForeignKey(UserProfile, verbose_name="编辑者", on_delete=models.CASCADE) img = models.ImageField(upload_to='img',verbose_name='封面') title = models.CharField(default="", max_length=30, verbose_name="新闻标题", help_text="新闻标题") desc = models.CharField(default="详细介绍", max_length=30, verbose_name="详细介绍", help_text="详细介绍") goods_desc = UEditorField(verbose_name=u"内容", imagePath="goods/images/", width=1000, height=900, filePath="goods/files/", default='') num=models.IntegerField(default=0,verbose_name='阅读量') fav=models.IntegerField(default=0,verbose_name='赞数') add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间") class Meta: verbose_name = "新闻" verbose_name_plural = verbose_name def __str__(self): return self.title def image_url(self): return mark_safe('<img src="/media/{0}" class="field_img">'.format(self.img)) image_url.short_description = '封面'
然后执行数据更新命令:
python manage.py makemigrations python manage.py migrate
3.关于我们和联系我们数据表
1.apps/user_operation/models.py:
from django.db import models from datetime import datetime from DjangoUeditor.models import UEditorField # Create your models here. class AboutMe(models.Model): """关于我们""" goods_desc = UEditorField(verbose_name=u"内容", imagePath="goods/images/", width=800, height=900, filePath="goods/files/", default='') add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间") class Meta: verbose_name = "关于我们" verbose_name_plural = verbose_name class CallMe(models.Model): """ 联系我们 """ name = models.CharField(default="", max_length=30, verbose_name="姓名", help_text="姓名") mail = models.CharField(default="", max_length=30, verbose_name="邮箱", help_text="邮箱") text=models.CharField(default="",max_length=500,verbose_name='内容',help_text='最长500字') add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间") class Meta: verbose_name = "联系我们" verbose_name_plural = verbose_name
然后执行数据更新命令:
python manage.py makemigrations python manage.py migrate