超越村后端开发(2:新建models.py+xadmin的引入)
1.新建Model
1.users数据
1.在apps/users/models.py中:
from datetime import datetime from django.db import models from django.contrib.auth.models import AbstractUser # Create your models here. class UserProfile(AbstractUser): """ 管理员 """ name=models.CharField(max_length=30,null=True,blank=True,verbose_name='姓名') 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 class WxUsers(models.Model): """ 微信用户 """ open_id=models.CharField(max_length=64,null=True,blank=True,verbose_name='OpenId') token=models.CharField(max_length=64,null=True,blank=True,verbose_name='token') nickName=models.CharField(max_length=30,null=True,blank=True,verbose_name='微信名') avatarUrl=models.CharField(max_length=200,null=True,blank=True,verbose_name='用户头像URL') gender = models.CharField(max_length=4, choices=((', verbose_name='性别') city=models.CharField(max_length=20,null=True,blank=True,verbose_name='用户所在市') longitude=models.CharField(max_length=10,null=True,blank=True,verbose_name='经度') latitude=models.CharField(max_length=10,null=True,blank=True,verbose_name='纬度') last_login_time = models.DateTimeField(default=datetime.now, 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.nickName
2.在settings.py中增加配置项:
AUTH_USER_MODEL='users.UserProfile'
3.执行数据更新命令:
python manage.py makemigrations python manage.py migrate
2.wish数据
1.apps/wish/models.py中:
from django.db import models from users.models import WxUsers,UserProfile from datetime import datetime from django.utils.safestring import mark_safe # Create your models here. class Wish(models.Model): """ 愿望表 """ wxuser=models.ForeignKey(WxUsers,on_delete=models.CASCADE) content=models.CharField(max_length=200,default='',verbose_name='愿望内容',help_text='最多200字') status=models.CharField(max_length=8, choices=((', verbose_name='阶段') end_time = models.DateTimeField(default=datetime.now, 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.content class News(models.Model): """ 新闻表 """ user=models.ForeignKey(UserProfile,on_delete=models.CASCADE) banner=models.ImageField(max_length=200, upload_to='banner/',verbose_name='轮播图') title=models.CharField(max_length=15,default='',verbose_name='新闻标题',help_text='最多15字') content=models.CharField(max_length=800,default='',verbose_name='新闻正文',help_text='最多800字') 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.banner)) image_url.short_description = '轮播图' class Images(models.Model): """ 新闻图片 """ image = models.ImageField(max_length=200, upload_to='images/') news=models.ForeignKey(News, verbose_name="新闻", blank=True,null=True,on_delete=models.CASCADE) add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间') class Meta: verbose_name = "新闻图片" verbose_name_plural = verbose_name def image_url(self): return mark_safe('<img src="/media/{0}" class="field_img">'.format(self.image)) image_url.short_description = '图片'
2.安装 pillow包(做图片处理的)
pip install pillow
3.执行数据更新命令:
python manage.py makemigrations python manage.py migrate
3.user_operation数据
1.apps/user_operation/models.py中:
from django.db import models from datetime import datetime from users.models import WxUsers,UserProfile from wish.models import Wish # Create your models here. class Message(models.Model): """ 评论表,消息表 """ user_send=models.ForeignKey(WxUsers,on_delete=models.CASCADE,verbose_name='发出方',related_name='send') user_receive = models.ForeignKey(WxUsers, on_delete=models.CASCADE, verbose_name='接收方', related_name='receive') wish=models.ForeignKey(Wish,on_delete=models.CASCADE,verbose_name='被评论的愿望') content = models.CharField(max_length=30, default='', verbose_name='内容', help_text='最多30字') is_read=models.BooleanField(default=False,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.content class Pick(models.Model): """ 点赞表 """ user = models.ForeignKey(WxUsers, on_delete=models.CASCADE, verbose_name='点赞者') wish = models.ForeignKey(Wish, on_delete=models.CASCADE, verbose_name='被点赞的愿望') action=models.BooleanField(default=True,verbose_name='行为',help_text='true是点赞false是取消点赞') add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间") class Meta: verbose_name='点赞表' verbose_name_plural=verbose_name def __str__(self): return self.user class Share(models.Model): """ 分享表 """ user = models.ForeignKey(WxUsers, on_delete=models.CASCADE, verbose_name='分享者') wish = models.ForeignKey(Wish, on_delete=models.CASCADE, 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.user class Notice(models.Model): """ 系统通知表(超越私信) """ admin=models.ForeignKey(UserProfile,on_delete=models.CASCADE) content = models.CharField(max_length=200, default='', verbose_name='内容', help_text='最多200字') add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间") class Meta: verbose_name='系统通知表' verbose_name_plural=verbose_name def __str__(self): return self.content
2.执行数据更新命令:
python manage.py makemigrations python manage.py migrate
2.引入xadmin
1.将xadmin文件夹放入extra_apps目录下:
2.在每个app下新建adminx.py
1.apps/users/目录下新建adminx.py:
import xadmin from xadmin import views from .models import WxUsers class BaseSetting(object): enable_themes = True use_bootswatch = True class GlobalSettings(object): site_title = "超越村后台管理系统" site_footer = "chaoyuecun" # menu_style = "accordion" class WxUsersAdmin(object): list_display = ["open_id", "token", "nickName", "gender","city","last_login_time","add_time"] list_filter = [ "gender","city","last_login_time","add_time"] search_fields = ["gender","city" ] xadmin.site.register(WxUsers, WxUsersAdmin) xadmin.site.register(views.BaseAdminView, BaseSetting) xadmin.site.register(views.CommAdminView, GlobalSettings)
2.apps/wish/目录下新建adminx.py:
import xadmin from .models import Wish, News,Images class WishAdmin(object): list_display = ['wxuser', 'content',"status","end_time","add_time"] list_filter =['wxuser', 'content',"status","end_time","add_time"] search_fields=['wxuser', 'content',"status"] class NewsAdmin(object): list_display = ['user', 'banner',"title","content","add_time"] list_filter =['user', 'banner',"title","content","add_time"] search_fields=['user', 'banner',"title","content"] class ImagesAdmin(object): list_display = ['image', 'news', "add_time"] list_filter = ['image', 'news', "add_time"] search_fields = ['image', 'news'] xadmin.site.register(Images, ImagesAdmin) xadmin.site.register(Wish, WishAdmin) xadmin.site.register(News, NewsAdmin)
3.apps/user_operation/目录下新建adminx.py:
import xadmin from .models import Message,Pick,Share,Notice class MessageAdmin(object): list_display = ['user_send', 'user_receive',"wish","content","is_read","add_time"] list_filter =['user_send', 'user_receive',"wish","content","is_read","add_time"] search_fields=['user_send', 'user_receive',"wish","content","is_read"] class PickAdmin(object): list_display = ['user', 'wish',"action","add_time"] list_filter =['user', 'wish',"action","add_time"] search_fields=['user', 'wish'] class ShareAdmin(object): list_display = ['user', 'wish',"add_time"] list_filter =['user', 'wish',"add_time"] search_fields=['user', 'wish'] class NoticeAdmin(object): list_display = ['admin', 'content',"add_time"] list_filter =['admin', 'content',"add_time"] search_fields=['admin', 'content'] xadmin.site.register(Message, MessageAdmin) xadmin.site.register(Pick, PickAdmin) xadmin.site.register(Share, ShareAdmin) xadmin.site.register(Notice, NoticeAdmin)
3.安装和配置xadmin依赖包
1.安装依赖包 django-crispy-forms django-reversion django-formtools future httplib2 six django-import-export
pip install django-crispy-forms django-reversion django-formtools future httplib2 six django-import-export
2.在settings中注册
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'users.apps.UsersConfig', 'wish.apps.WishConfig', 'user_operation.apps.UserOperationConfig', 'crispy_forms', 'xadmin' ]
3.安装用来操作和导出Excel文件的包 xlwt xlsxwriter
pip install xlwt xlsxwriter
4.打开终端,执行数据更新命令,生成xadmin默认的表
python manage.py makemigrations python manage.py migrate
5.配置url,在chaoyuecun/urls.py:
from django.contrib import admin from django.urls import path import xadmin urlpatterns = [ # path('admin/', admin.site.urls), path('xadmin/', xadmin.site.urls), ]
6.创建超级用户
python manage.py createsuperuser Username: admin 邮箱: Password: Password (again):
创建完毕,启动项目,然后访问
http://127.0.0.1:8000/xadmin/
就可以登录xadmin后台了,但是此时后台还是英文的。
7.将语言设定为中文
1.在settings中修改
LANGUAGE_CODE = 'zh-hans' TIME_ZONE = 'Asia/Shanghai' USE_I18N = True USE_L10N = True USE_TZ = False
2.在每个app下的apps.py中做修改,加上verbose_name
1.apps/users/apps.py:
from django.apps import AppConfig class UsersConfig(AppConfig): name = 'users' verbose_name = '用户'
1.apps/user_operation/apps.py:
from django.apps import AppConfig class UserOperationConfig(AppConfig): name = 'user_operation' verbose_name = '用户操作'
1.apps/wish/apps.py:
from django.apps import AppConfig class WishConfig(AppConfig): name = 'wish' verbose_name = '愿望'
这时再看xadmin界面,已经变成中文的了。
超越村后端开发(2:新建models.py+xadmin的引入)的更多相关文章
- 超越村后端开发(3:安装djangorestframework+序列化+API开发前期准备)
1.安装djangorestframework 1.安装djangorestframework及其依赖包markdown.django-filter. pip install djangorestfr ...
- 超越村后端开发(4:API开发)
1.users相关的api开发 1.在settings中添加APPID,SECRET 2.在apps/users/views.py内: from chaoyuecun.settings import ...
- 超越村后端开发(5:远程同步本地与服务器端的MySQL数据库)
1.同步MySQL数据库 服务器选用的华为云,安装了Ubuntu18.04,华为云默认是以root用户登录的. 1.使用Xshell6连接华为云 ls 2.Ubuntu18.04安装MySQL5.7 ...
- 广告小程序后端开发(2.Models设计)
1.users的数据表设计: 1.重建用户表: 1.在settings中配置: AUTH_USER_MODEL='users.UserProfile' 2.apps/users/models.py中建 ...
- 越光后端开发——ygapi(2.新建Model)
1.新建Model 1.users数据 1.在apps/users/models.py中: from datetime import datetime from django.db import mo ...
- $2015 武汉森果公司web后端开发实习日记----书写是为了更好的思考
找暑期实习,3月份分别投了百度和腾讯的实习简历,都止步于笔试,总结的主要原因有两点:基础知识不扎实,缺乏项目经验.后来到拉勾网等网站上寻找实习,看了很多家,都还是处于观望状态.后来参加了武汉实习吧在大 ...
- 【Python全栈-后端开发】Django入门基础
Django基础知识 一. 什么是web框架? 框架,即framework,特指为解决一个开放性问题而设计的具有一定约束性的支撑结构,使用框架可以帮你快速开发特定的系统,简单地说,就是你用别人搭建好的 ...
- Python后端开发要求
关于Python后端开发要求 一.对Python有兴趣,熟悉Python(标准库) 最好阅读过源码 了解Python的优化(熟悉pypy更佳) 二.至少至少一门语言(不说"精通") ...
- 不在models.py中的models
概述 如何让你定义的model不在models.py中 在app的models目录中的models 你新建一个app后这个models.py就会自动建立,里面只有几行代码.那么如果是一个中大型项目,每 ...
随机推荐
- JavaScript实现获取两个排序数组的中位数算法示例
本文实例讲述了JavaScript排序代码实现获取两个排序数组的中位数算法.分享给大家供大家参考,具体如下: 题目 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个 ...
- fab 菜单实现之前传-钟表表盘
个人很喜欢谷歌的material design,很喜欢但是没有动手弄过,今天想动手操作一下Floating Action Button菜单,网上有很多种:圆形.扇形.射线.直线等.我想在一个例子中用到 ...
- 使用Boostrap框架写一个登录\注册界面
Bootstrap是一个Web前端开发框架,使用它提供的css.js文件可以简单.方便地美化HTML控件.一般情况下,对控件的美化需要我们自己编写css代码,并通过标签选择器.类选择器.ID选择器为指 ...
- 使用免费 mongodb数据库 + 免费node.js服务器搭建小程序接口
由于微信的小程序只支持不带端口的域名接口,不支持IP地址和接口,所以我们需要映射到80端口并绑定备案过的域名才能被微信小程序访问到.简单点就是接口需要 https 协议才行,找了许久的免费的数据库与n ...
- json-sever 配置与应用
先安装 node.js. 全局安装 json-server: npm install json-server -g 检查是否安装成功: json-server -h 安装好后,json-server的 ...
- HTTP中GET和POST的区别主要是那些,面试中可以加分的该说那些?
面试回答: GET请求在URL中传送的参数是有长度限制的,而POST没有. GET比POST更不安全,因为参数直接暴露在URL上,所以不能用来传递敏感信息. GET参数通过URL传递,POST放在Re ...
- 深入浅出KNN算法(一) KNN算法原理
一.KNN算法概述 KNN可以说是最简单的分类算法之一,同时,它也是最常用的分类算法之一,注意KNN算法是有监督学习中的分类算法,它看起来和另一个机器学习算法Kmeans有点像(Kmeans是无监督学 ...
- 关于LeetCode上链表题目的一些trick
最近在刷leetcode上关于链表的一些高频题,在写代码的过程中总结了链表的一些解题技巧和常见题型. 结点的删除 指定链表中的某个结点,将其从链表中删除. 由于在链表中删除某个结点需要找到该结点的前一 ...
- redis5.0.4多实例安装
一.安装第一个实例 https://www.cnblogs.com/qq931399960/p/10584877.html 二.拷贝配置文件 cp /etc/redis.conf /etc/redis ...
- 华为笔记HOSTS,便于访问云端存储
# Copyright (c) - Microsoft Corp. # # This is a sample HOSTS file used by Microsoft TCP/IP for Windo ...