【VUE】会员管理(增删改查)

devtools/2024/10/9 5:04:56/

前端

router/index.js

import { createRouter, createWebHistory } from 'vue-router'
import {userInfoStore} from "@/stores/user.js";const router = createRouter({history: createWebHistory(import.meta.env.BASE_URL),routes: [{path: '/login',name: 'login',component: () => import('../views/LoginView.vue')},{path: '/admin',name: 'admin',component: () => import('../views/AdminView.vue'),children: [{path: "",redirect: {name: "home"}},{path: "home",name: "home",component: () => import('../views/HomeView.vue')},{path: "vip",name: "vip",component: () => import('../views/VipView.vue')}]}]
})
router.beforeEach(function (to,from,next) {// 1.访问登录页面,不需要登录就可以直接去查看if (to.name === "login") {next()return}// 2.检查用户登录状态,登录成功,继续往后走next();未登录,跳转至登录页面// let username = localStorage.getItem("name")const store = userInfoStore()if (!store.userId){next({name:"login"})return;}// 3.登录成功且获取到用户信息,继续向后访问next()
})export default router

views/VipView.vue

<template><h1>会员管理</h1><button @click="doEdit">编辑</button><table border="1"><thead><tr><th>ID</th><th>姓名</th><th>级别</th><th>积分</th><th>操作</th></tr></thead><tbody><tr v-for="(item,idx) in dataList"><td>{{ item.id }}</td><td>{{ item.name }}</td><td>{{ item.level_text }}</td><td>{{ item.score }}</td><td><a>编辑</a>|<a>删除</a></td></tr></tbody></table><div v-show="dialog" class="mask"></div><div v-show="dialog" class="dialog"><input type="text"/><p><button>保存</button><button @click="dialog=false">取消</button></p></div>
</template><script setup>
import {ref} from "vue";const dataList = ref([{id: 1, name: "cc", age: 18, level_text: "SVIP", score: 1000}])
const dialog = ref(false)function doEdit() {dialog.value = true
}
</script><style scoped>
.mask {position: fixed;top: 0;bottom: 0;left: 0;right: 0;background-color: black;opacity: 0.8;z-index: 998;
}.dialog {position: fixed;top: 200px;right: 0;left: 0;width: 400px;height: 300px;background-color: white;margin: 0 auto;z-index: 9999;
}
</style>

后端

GET     http://127.0.0.1:8000/api/vip/          -> 会员列表
POST    http://127.0.0.1:8000/api/vip/          -> 新增会员(请求体中传入数据)
DELETE  http://127.0.0.1:8000/api/vip/会员ID/    -> 删除会员
PUT     http://127.0.0.1:8000/api/vip/会员ID/    -> 更新会员(请求体中传入数据)

urls.py

"""
from django.urls import path
from api.views import account
from api.views import vipurlpatterns = [path('api/auth/', account.AuthView.as_view()),path('api/vip/', vip.VipView.as_view()),path('api/vip/<int:vid>/', vip.VipDetailView.as_view()),
]

models.py

from django.db import modelsclass UserInfo(models.Model):username = models.CharField(verbose_name="用户名", max_length=64)password = models.CharField(verbose_name="密码", max_length=64)token = models.CharField(verbose_name="token", max_length=64, null=True, blank=True)class Vip(models.Model):""" 会员管理 """name = models.CharField(verbose_name="用户名", max_length=32)level = models.IntegerField(verbose_name="级别", choices=[(1, "VIP"), (2, "SVIP"), (3, "SSVIP")])score = models.IntegerField(verbose_name="积分")

views/vip.py

from rest_framework.views import APIView
from api import models
from rest_framework import serializers
from rest_framework.response import Responseclass VipSerializers(serializers.ModelSerializer):level_text = serializers.CharField(source="get_level_display", read_only=True)class Meta:model = models.Vipfields = "__all__"class VipView(APIView):def get(self, request):# 会员列表queryset = models.Vip.objects.all().order_by("id")ser = VipSerializers(instance=queryset, many=True)return Response({"code": 0, "data": ser.data})def post(self, request):# 新增passclass VipDetailView(APIView):def delete(self, request, vid):# 删除passdef put(self, request, vid):# 修改pass

前端:VipView.vue

<template><h1>会员管理</h1><button @click="doEdit">编辑</button><table border="1"><thead><tr><th>ID</th><th>姓名</th><th>级别</th><th>积分</th><th>操作</th></tr></thead><tbody><tr v-for="(item,idx) in dataList"><td>{{ item.id }}</td><td>{{ item.name }}</td><td>{{ item.level_text }}</td><td>{{ item.score }}</td><td><a>编辑</a>|<a>删除</a></td></tr></tbody></table><div v-show="dialog" class="mask"></div><div v-show="dialog" class="dialog"><input type="text"/><p><button>保存</button><button @click="dialog=false">取消</button></p></div>
</template><script setup>
import {ref, onMounted} from "vue";
import _axios from "@/plugins/axios.js";const dataList = ref([{id: 1, name: "cc", age: 18, level_text: "SVIP", score: 1000}])
const dialog = ref(false)onMounted(function (){_axios.get("/api/vip/").then((res) => {console.log(res.data)dataList.value = res.data.data})
})
function doEdit() {dialog.value = true
}
</script><style scoped>
.mask {position: fixed;top: 0;bottom: 0;left: 0;right: 0;background-color: black;opacity: 0.8;z-index: 998;
}.dialog {position: fixed;top: 200px;right: 0;left: 0;width: 400px;height: 300px;background-color: white;margin: 0 auto;z-index: 9999;
}
</style>

删除

vip.py

from rest_framework.views import APIView
from api import models
from rest_framework import serializers
from rest_framework.response import Responseclass VipSerializers(serializers.ModelSerializer):level_text = serializers.CharField(source="get_level_display", read_only=True)class Meta:model = models.Vipfields = "__all__"class VipView(APIView):def get(self, request):# 会员列表queryset = models.Vip.objects.all().order_by("id")ser = VipSerializers(instance=queryset, many=True)return Response({"code": 0, "data": ser.data})def post(self, request):# 新增passclass VipDetailView(APIView):def delete(self, request, vid):# 删除models.Vip.objects.filter(id=vid).delete()return Response({"code": 0})def put(self, request, vid):# 修改pass

cors.py

from django.utils.deprecation import MiddlewareMixinclass CorsMiddleware(MiddlewareMixin):def process_response(self, request, response):response['Access-Control-Allow-Origin'] = "*"response['Access-Control-Allow-Headers'] = "*"response['Access-Control-Allow-Methods'] = "*"return response

前端
VipView.vue

<template><h1>会员管理</h1><button @click="doEdit">新增</button><table border="1"><thead><tr><th>ID</th><th>姓名</th><th>级别</th><th>积分</th><th>操作</th></tr></thead><tbody><tr v-for="(item,idx) in dataList"><td>{{ item.id }}</td><td>{{ item.name }}</td><td>{{ item.level_text }}</td><td>{{ item.score }}</td><td><a>编辑</a>|<button @click="doDelete(item.id, idx)">删除</button></td></tr></tbody></table><div v-show="dialog" class="mask"></div><div v-show="dialog" class="dialog"><input type="text"/><p><button>保存</button><button @click="dialog=false">取消</button></p></div>
</template><script setup>
import {ref, onMounted} from "vue";
import _axios from "@/plugins/axios.js";const dataList = ref([{id: 1, name: "cc", age: 18, level_text: "SVIP", score: 1000}])
const dialog = ref(false)onMounted(function () {_axios.get("/api/vip/").then((res) => {console.log(res.data)dataList.value = res.data.data})
})function doEdit() {dialog.value = true
}function doDelete(vid, idx) {_axios.delete(`/api/vip/${vid}/`).then((res) => {// console.log(res.data)if (res.data.code === 0) {dataList.value.splice(idx, 1)}})
}
</script><style scoped>
.mask {position: fixed;top: 0;bottom: 0;left: 0;right: 0;background-color: black;opacity: 0.8;z-index: 998;
}.dialog {position: fixed;top: 200px;right: 0;left: 0;width: 400px;height: 300px;background-color: white;margin: 0 auto;z-index: 9999;
}
</style>

http://www.ppmy.cn/devtools/123203.html

相关文章

随笔(四)——代码优化

文章目录 前言1.原本代码2.新增逻辑3.优化逻辑 前言 原逻辑&#xff1a;后端data数据中返回数组&#xff0c;数组中有两个对象&#xff0c;一个是属性指标&#xff0c;一个是应用指标&#xff0c;根据这两个指标展示不同的多选框 1.原本代码 getIndicatorRange(indexReportLi…

系统架构设计师论文《论企业集成平台的理解与应用》精选试读

论文真题 企业集成平台&#xff08;Enterprise Imtcgation Plaform,EIP)是支特企业信息集成的像环境&#xff0c;其主要功能是为企业中的数据、系统和应用等多种对象的协同行提供各种公共服务及运行时的支撑环境。企业集成平台能够根据业务模型的变化快速地进行信息系统的配置…

D27【 python 接口自动化学习】- python 基础之判断与循环

day27 判断和循环中常见错误 学习日期&#xff1a;20241004 学习目标&#xff1a;判断与循环&#xfe63;-38 避坑指南&#xff1a;判断和循环中的常见错误 学习笔记&#xff1a; 循环过程中改变遍历次数 遍历中修改列表导致误操作 循环嵌套中的缩进导致运行语句有差别 总结…

【重识云原生】第六章容器6.2.2节——K8S架构剖析

1. K8s架构综述 Kubernetes 最初源于谷歌内部的 Borg&#xff0c;提供了面向应用的容器集群部署和管理系统。Kubernetes 的目标旨在消除编排物理/虚拟计算、网络和存储等基础设施资源的负担&#xff0c;并使应用程序运营商和开发人员完全将重点放在以容器为中心的原语上进行自助…

解决方案:Pandas里面的loc跟iloc,有什么区别

文章目录 一、现象二、解决方案案例使用loc使用iloc 简单总结 一、现象 在用Pandas库处理数据的时候&#xff0c;久而久之不用loc跟iloc&#xff0c;难免会有些混乱记混 二、解决方案 在Pandas中&#xff0c;loc和iloc是两种常用的数据选择方法&#xff0c;它们的主要区别在…

【C++指南】类和对象(二):类的默认成员函数——全面剖析 :构造函数

&#x1f493; 博客主页&#xff1a;倔强的石头的CSDN主页 &#x1f4dd;Gitee主页&#xff1a;倔强的石头的gitee主页 ⏩ 文章专栏&#xff1a;《C指南》 期待您的关注 ​ 阅读本篇文章之前&#xff0c;你需要具备的前置知识&#xff1a;类和对象的基础 点击下方链接 【C指南…

Qt QWidget控件

目录 一、概述 二、Qwidget常用属性及函数介绍 2.1 enable 2.2 geometry 2.3 windowTitle 2.4 windowIcon 2.5 cursor 2.6 font 设置字体样式 2.7 toolTip 2.8 focusPolicy焦点策略 2.9 styleSheet 一、概述 widget翻译而来就是小控件&#xff0c;小部件。…

1.一、MyBatis入门

一、MyBatis入门 我们做为后端程序开发人员&#xff0c;通常会使用Java程序来完成对数据库的操作。Java程序操作数据库&#xff0c;现在主流的方式是&#xff1a;Mybatis。 一、什么是MyBatis? MyBatis官网的解释&#xff1a; MyBatis 是一款优秀的持久层框架&#xff0c;它…