当前位置:网站首页>02-项目实战之后台员工信息管理
02-项目实战之后台员工信息管理
2022-07-06 09:13:00 【As。】
- 创建员工信息数据表;user (已创建过了)
- 定义模型Model类;进入myobject/myadmin/models.py;由于user表中id是自增的,所以不需要再定义
from django.db import models
from datetime import datetime
#员工账号信息模型
class User(models.Model):
username = models.CharField(max_length=50) #员工账号
nickname = models.CharField(max_length=50) #昵称
password_hash = models.CharField(max_length=100)#密码
password_salt = models.CharField(max_length=50) #密码干扰值
status = models.IntegerField(default=1) #状态:1正常/2禁用/9删除
create_at = models.DateTimeField(default=datetime.now) #创建时间
update_at = models.DateTimeField(default=datetime.now) #修改时间
def toDict(self):
return {
'id': self.id, 'username': self.username, 'nickname': self.nickname, 'password_hash': self.password_hash,' password_salt': self.password_salt, 'status': self.status, 'create_at': self.create_at.strftime('%Y-%m-%d %H:%M:%S'), 'update_at': self.update_at.strftime('%Y-%m-%d %H:%M:%S')}
class Meta:
db_table = "user" # 更改表名
- 将myobject/myadmin/index.py 复制一份到当前路径,名称改为user.py(每个页面的视图单独管理),添加6个函数(增删改查操作)
#员工信息管理的视图文件
from django.shortcuts import render
from django.http import HttpResponse
from myadmin.models import User
# Create your views here.
def index(request): #浏览信息
pass
def add(request): #加载信息添加表单
pass
def insert(request): #执行信息添加
pass
def delete(request, uid=0): #执行信息删除
pass
def edit(request, uid=0): #加载信息编辑表单
pass
def update(request, uid): #执行信息编辑
pass
- 配置url路由`
#后台管理子路由文件
from django.urls import path
from myadmin.views import index, user
urlpatterns = [
path('', index.index, name="myadmin_index"), #后台首页
#员工信息管理路由
path('user/', user.index, name="myadmin_user_index"), #浏览
path('user/add', user.add, name="myadmin_user_add"), #添加表单
path('user/insert', user.insert, name="myadmin_user_insert"), #执行添加
path('user/del/<int:uid>', user.delete, name="myadmin_user_delete"), #执行删除
path('user/edit/<int:uid>', user.edit, name="myadmin_user_edit"), #加载编辑表单
path('user/update/<int:uid>', user.update, name="myadmin_user_update"), #执行编辑
]
- 在模板中,完善浏览操作;在\myobject\templates\myadmin里新建一个user文件夹,在user文件夹中创建一个index.html文件;myobject/myadmin/user.py修改如下:
def index(request): #浏览信息
umod = User.objects
ulist = umod.all()
context = {
"userlist": ulist}
return render(request, "myadmin/user/index.html", context)
- 在\myobject\templates\myadmin\user\index.html中进行继承操作
{
% extends 'myadmin/base.html' %}
{
% block main_body %}
{
% endblock %}
- 在父模板(base.html)进行反解析操作
13.员工信息浏览
14.修改员工中的状态信息,判断status状态
15.分页,在user.py中操作
#员工信息管理的视图文件
from django.shortcuts import render
from django.http import HttpResponse
from myadmin.models import User
from django.core.paginator import Paginator #分页导包
def index(request,pIndex=1): #浏览信息
umod = User.objects #实例化对象
#ulist = umod.all() #查询所有数据
ulist = umod.filter(status__lt=9) #过滤信息,过滤status状态 小于9的 展示在页面
#执行分页处理
pIndex = int(pIndex)
page = Paginator(ulist, 5) #以每页5条数据分页
maxpages = page.num_pages #获取最大页数
#判断当前页是否越界
if pIndex > maxpages:
pIndex = maxpages
if pIndex < 1:
pIndex = 1
list2 = page.page(pIndex) #获取当前页数据
plist = page.page_range #获取页面列表信息
context = {
"userlist": list2, 'plist':plist, 'pIndex':pIndex, 'maxpages': maxpages}
return render(request, "myadmin/user/index.html", context) #模板渲染
def add(request): #加载信息添加表单
pass
def insert(request): #执行信息添加
pass
def delete(request, uid=0): #执行信息删除
pass
def edit(request, uid=0): #加载信息编辑表单
pass
def update(request, uid): #执行信息编辑
pass
15.在index.html中获取页面信息
16.搜索框 (模糊搜索名称和昵称,以及状态)
#员工信息管理的视图文件
from django.shortcuts import render
from django.http import HttpResponse
from myadmin.models import User
from django.core.paginator import Paginator #分页导包
from django.db.models import Q #搜索条件为或的
def index(request,pIndex=1): #浏览信息
umod = User.objects #实例化对象
#ulist = umod.all() #查询所有数据
ulist = umod.filter(status__lt=9) #过滤信息,过滤status状态 小于9的 展示在页面
mywhere=[]
#获取并判断搜索条件
kw = request.GET.get("keyword", None)
if kw:
ulist = ulist.filter(Q(username__contains=kw) | Q(nickname__contains=kw))
mywhere.append('keyword='+kw)
# 获取、判断并封装状态status搜索条件
status = request.GET.get('status', '')
if status != '':
ulist = ulist.filter(status=status)
mywhere.append("status=" + status)
#执行分页处理
pIndex = int(pIndex)
page = Paginator(ulist, 5) #以每页5条数据分页
maxpages = page.num_pages #获取最大页数
#判断当前页是否越界
if pIndex > maxpages:
pIndex = maxpages
if pIndex < 1:
pIndex = 1
list2 = page.page(pIndex) #获取当前页数据
plist = page.page_range #获取页面列表信息
context = {
"userlist": list2, 'plist':plist, 'pIndex':pIndex, 'maxpages': maxpages, "mywhere": mywhere}
return render(request, "myadmin/user/index.html", context) #模板渲染
def add(request): #加载信息添加表单
pass
def insert(request): #执行信息添加
pass
def delete(request, uid=0): #执行信息删除
pass
def edit(request, uid=0): #加载信息编辑表单
pass
def update(request, uid): #执行信息编辑
pass
边栏推荐
- Redis的基础使用
- Install MySQL for Ubuntu 20.04
- Install mysql5.5 and mysql8.0 under windows at the same time
- Timestamp with implicit default value is deprecated error in MySQL 5.6
- Ansible实战系列一 _ 入门
- La table d'exportation Navicat génère un fichier PDM
- MySQL transaction log
- Postman uses scripts to modify the values of environment variables
- How to find the number of daffodils with simple and rough methods in C language
- Solve the problem that XML, YML and properties file configurations cannot be scanned
猜你喜欢
Postman environment variable settings
C language advanced pointer Full Version (array pointer, pointer array discrimination, function pointer)
Detailed reading of stereo r-cnn paper -- Experiment: detailed explanation and result analysis
Other new features of mysql18-mysql8
Redis的基础使用
MySQL19-Linux下MySQL的安装与使用
CSDN Q & a tag skill tree (V) -- cloud native skill tree
windows无法启动MYSQL服务(位于本地计算机)错误1067进程意外终止
Django运行报错:Error loading MySQLdb module解决方法
CSDN question and answer tag skill tree (I) -- Construction of basic framework
随机推荐
IDEA 导入导出 settings 设置文件
【博主推荐】SSM框架的后台管理系统(附源码)
MySQL23-存儲引擎
MySQL23-存储引擎
Windchill configure remote Oracle database connection
Mysql34 other database logs
[recommended by bloggers] background management system of SSM framework (with source code)
Mysql21 user and permission management
C language string function summary
【博主推荐】asp.net WebService 后台数据API JSON(附源码)
How to change php INI file supports PDO abstraction layer
The virtual machine Ping is connected to the host, and the host Ping is not connected to the virtual machine
MySQL主从复制、读写分离
Mysql24 index data structure
[untitled]
[BMZCTF-pwn] 12-csaw-ctf-2016-quals hungman
MySQL 20 MySQL data directory
Have you mastered the correct posture of golden three silver four job hopping?
Unicode decodeerror: 'UTF-8' codec can't decode byte 0xd0 in position 0 successfully resolved
CSDN Q & a tag skill tree (V) -- cloud native skill tree