首页 > 其他分享 >Vue_Django 登录注册+图书管理系统

Vue_Django 登录注册+图书管理系统

时间:2023-06-11 16:13:33浏览次数:49  
标签:username axios name 管理系统 res back Django Vue dic

Vue前端

注册页面

点击查看代码
<template>
  <div class="register">
    <el-row :gutter="20">
      <el-col :span="12" :offset="6">
        <div class="grid-content bg-purple">
          <h1>注册页面</h1>
          <el-form  status-icon  ref="ruleForm" label-width="100px"
                   class="demo-ruleForm">
            <el-form-item label="用户名" prop="username">
              <el-input v-model.number="username"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="pass">
              <el-input type="password" v-model="pass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="checkPass">
              <el-input type="password" v-model="checkPass" autocomplete="off"></el-input>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="submitForm">提交</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import axios from "axios";

export default {
  data() {
    return {
      username:'',
      pass:'',
      checkPass:'',
    };
  },
  methods: {
    submitForm() {
      axios.post('http://127.0.0.1:8000/register/', {
        username: this.username,
        pass:this.pass,
        checkPass:this.checkPass
      })
          .then(function (res) {
            if (res.data.code===100){
              location.href = 'http://localhost:8080/login'
            }else {
              alert(res.data.msg)
            }
          })
    },
  }
}
</script>

登录

点击查看代码
<template>
  <div class="login">
    <el-row :gutter="20">
      <el-col :span="12" :offset="6">
        <div class="grid-content bg-purple">
          <h1>登录页面</h1>
          <el-form status-icon ref="ruleForm" label-width="100px"
                   class="demo-ruleForm">
            <el-form-item label="用户名" prop="username">
              <el-input v-model.number="username"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="pass">
              <el-input type="password" v-model="pass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm">提交</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import axios from "axios";

export default {
  data() {
    return {
      username:'',
      pass:'',
    };
  },
  methods: {
    submitForm() {
      axios.post('http://127.0.0.1:8000/login/', {
        username: this.username,
        pass:this.pass,
      })
          .then(function (res) {
            if (res.data.code===100){
              location.href = 'http://localhost:8080/'
            }else {
              alert(res.data.msg)
            }
          })
    },
  }
}
</script>

查看所有图书

点击查看代码
<template>
  <div class="name">
    <h1>图书管理</h1>
    <el-row :gutter="20">
      <el-col :span="12" :offset="6">
        <div class="grid-content bg-purple">
          <el-button type="success" @click="handleCreate">增加图书</el-button>
          <el-table
              :data="tableData"
              style="width: 100%">
            <el-table-column
                prop="id"
                label="图书id"
                width="180">
            </el-table-column>
            <el-table-column
                prop="name"
                label="图书名称"
                width="180">
            </el-table-column>
            <el-table-column
                prop="price"
                label="价格">
            </el-table-column>
            <el-table-column
                prop="operate"
                label="操作">
              <template slot-scope="scope">
                <el-button
                    size="mini"
                    type="primary"
                    @click="handleEdit(scope.$index, scope.row)">修改
                </el-button>
                <el-button
                    size="mini"
                    type="danger"
                    @click="handleDelete(scope.$index, scope.row)">删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import axios from "axios";

export default {
  name: 'HomeView',
  data() {
    return {
      // tableData: [{
      //   id:'',
      //   name:'',
      //   price:''
      // }],
      tableData:[]
    }
  },
  created() {
    axios.get('http://127.0.0.1:8000/book').then(res => {
      this.tableData = res.data
      // console.log(res.data)
      // res.data.forEach((value, index) => {
      //   this.tableData.id = index
      //   this.tableData.name = value.name
      //   this.tableData.price = value.price
      //   console.log(this.tableData)
      // })
    })
  },
  methods: {
    handleCreate() {
      this.$router.push('/create')
    },
    handleEdit(index, row) {
      // console.log(index, row);
      // console.log(row.name)
      // console.log(row.price)
      this.$router.push({name: 'update', params: {id: row.id, bookName: row.name, bookPrice: row.price}})
    },
    handleDelete(index, row) {
      console.log(index, row);
      console.log(row.id)
      axios.delete('http://127.0.0.1:8000/book/' + row.id).then(res => {
        this.$message({
          message: '删除成功',
          type: 'success',
          duration: 1000, // 设置为 1 秒钟
          onClose: () => {
            setTimeout(() => {
              location.reload()
            })
          }
        });
      })
    }
  }
}
</script>

新增图书

点击查看代码
<template>
  <div class="create">
    <el-row :gutter="20">
      <el-col :span="12" :offset="6">
        <div class="grid-content bg-purple" style="margin-top: 40px">
          <h1>添加图书</h1>
          <el-row :gutter="20">
            <el-col :span="14" :offset="5">
              <el-input v-model="name" placeholder="图书名字"></el-input>
              <el-input v-model="price" placeholder="图书价格" style="margin-top: 20px"></el-input>
              <el-button type="success" @click="handleSend" style="float: right;margin-top: 20px">确认添加</el-button>
            </el-col>
          </el-row>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import axios from "axios";

export default {
  name: "BookCreateView",
  data() {
    return {
      name: '',
      price: ''
    }
  },
  methods: {
    handleSend() {
      axios.post('http://127.0.0.1:8000/book/', {
        name: this.name,
        price: this.price,
      }).then(res => {
        this.$message({
          message: '添加成功',
          type: 'success',
          duration: 1000, // 设置为 1 秒钟
          onClose: () => {
            setTimeout(() => {
              this.$router.push('/')
            })
          }
        });
      }).catch(error => {
        console.log(error); // 输出错误信息
      });
    }
  }
}
</script>

修改图书

点击查看代码
<template>
  <div class="update">
    <el-row :gutter="20">
      <el-col :span="12" :offset="6">
        <div class="grid-content bg-purple" style="margin-top: 40px">
          <h1>修改图书</h1>
          <el-row :gutter="20">
            <el-col :span="14" :offset="5">
              <el-input v-model="name" :placeholder="bookName"></el-input>
              <el-input v-model="price" :placeholder="bookPrice" style="margin-top: 20px"></el-input>
              <el-button type="success" @click="handleSend(bookId)" style="float: right;margin-top: 20px">确认修改
              </el-button>
            </el-col>
          </el-row>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import axios from "axios";

export default {
  name: "BookUpdateView",
  data() {
    return {
      name: '',
      price: '',
      bookId: '',
      bookName: '',
      bookPrice: ''
    }
  },
  created() {
    // console.log(this.$route.params.id)
    // console.log(this.$route.params.bookName)
    // console.log(this.$route.params.bookPrice)
    this.bookId = this.$route.params.id
    this.bookName = this.$route.params.bookName
    this.bookPrice = this.$route.params.bookPrice
  },
  methods: {
    handleSend(bookId) {
      axios.put('http://127.0.0.1:8000/book/' + bookId + '/', {
        name: this.name,
        price: this.price,
      }).then(response => {
        console.log(response.data); // 输出成功修改的图书信息
        this.$message({
          message: '修改成功',
          type: 'success',
          duration: 1000, // 设置为 1 秒钟
          onClose: () => {
            setTimeout(() => {
              this.$router.push('/')
            })
          }
        });
      }).catch(error => {
        console.log(error); // 输出错误信息
        alert('不能为空')
      });
    }
  }
}
</script>

Django后端

注册

from rest_framework.views import APIView
from django.contrib import auth
from django.contrib.auth.models import User

class RegisterView(APIView):
    def post(self, request):
        back_dic = {'code': 100, 'msg': '注册成功'}
        res = json.loads(request.body)
        username = res.get('username')
        password = res.get('pass')
        repassword = res.get('checkPass')
        user_obj = User.objects.filter(username=username)
        if user_obj:
            back_dic['code'] = 101
            back_dic['msg'] = '用户名已存在'
            return Response(back_dic)
        if password != repassword:
            back_dic['code'] = 101
            back_dic['msg'] = '两次密码不一致'
            return Response(back_dic)
        if not username:
            back_dic['code'] = 101
            back_dic['msg'] = '用户名不能为空'
            return Response(back_dic)

        User.objects.create_user(username=username, password=password)
        return Response(back_dic)

登录

from rest_framework.views import APIView
from django.contrib import auth
from django.contrib.auth.models import User

class LoginView(APIView):
    def post(self, request):
        back_dic = {'code': 100, 'msg': '注册成功'}
        res = json.loads(request.body)
        username = res.get('username')
        password = res.get('pass')
        user_obj = auth.authenticate(request, username=username, password=password)
        if user_obj:
            return Response(back_dic)
        else:
            back_dic['code'] = 101
            back_dic['msg'] = '用户名或密码错误'
            return Response(back_dic)

图书接口(增删改查)

from rest_framework.viewsets import ModelViewSet
from .models import Books
from .serializer import BookSerializer

class BookDetailView(ModelViewSet):
    queryset = Books.objects.all()
    serializer_class = BookSerializer

标签:username,axios,name,管理系统,res,back,Django,Vue,dic
From: https://www.cnblogs.com/XxMa/p/17473065.html

相关文章

  • Spring Boot&Vue3前后端分离实战wiki知识库系统<八>--分类管理功能开发二
    接着上一次SpringBoot&Vue3前后端分离实战wiki知识库系统<七>--分类管理功能开发的分类功能继续完善。分类编辑功能优化:概述:现在分类编辑时的界面长这样:很明显目前的父分类的展现形式不太人性,这里需要指定父分类的id才可以,对于用户来说这种交互是反人道的,用户怎么知道父分类......
  • Django3中分组查询的一个坑
    最近在学习django的分组查询,发现使用通常的values加annotate方法,获取不到我想要的结果,后来通过查询官方文档得到答案一、问题描述1.1模型fromdjango.dbimportmodels#Createyourmodelshere.classGoods(models.Model):g_name=models.CharField(max_length=......
  • Django的staticfiles库
    staticfiles库是Django提供的一个用于管理静态文件的库,它提供了一些工具和函数来帮助开发者在Django应用程序中管理和提供静态文件服务。在Django应用程序中,静态文件通常包括CSS样式文件、JavaScript脚本文件、图像文件以及其他一些资源文件。这些静态文件通常需要被服......
  • vue报错Invalid VNode type: undefined
    报错项目启动后,部分内容没有显示出来。打开console后,显示[Vuewarn]:InvalidVNodetype:undefined(undefined)处理引入“defineAsyncComponent”实现异步引入。import{defineAsyncComponent}from'vue'。问题解决了。......
  • Vue插件:Vue-resource github搜索示例
     1:安装插件  vue-resourcevue的插件库,在vue1.0年代使用几率很高2:界面效果3:代码信息说明:该示例代码基本上是与《“Vue中通过事件总线方式组件间传递数据及调用Vue脚手架中的axios数据调用方式获取github提供的用户接口数据信息”》一文中的代码相同。        故:此处......
  • Vue 中通过事件总线方式组件间传递数据及调用 Vue脚手架中的axios数据调用方式获取git
    1:看界面效果2:代码结构3:代码内容3.1:引入第三方css样式表:bootstrap.css/*!*Bootstrapv3.3.5(http://getbootstrap.com)*Copyright2011-2015Twitter,Inc.*LicensedunderMIT(https://github.com/twbs/bootstrap/blob/master/LICENSE)*//*!normalize.cssv3.0.3|......
  • vue后台管理系统实现全屏展示
    效果图展示直接上代码<!--全屏显示--><divclass="btn-fullscreen"@click="handleFullScreen"><el-tooltipeffect="dark":content="fullscreen?`取消全屏`:`全屏`"placement="bottom">......
  • django
    1常用指令pipinstalldjango-ihttps://pypi.douban.com/simple/#使用豆瓣镜像安装djangodjango-adminstartprojectmysite#创建个人项目pythonmanage.pyrunserver#启动项目pythonmanage.pystartapppolls#创建投票应用pythonmanage.pymakemigrations#......
  • Vue跨域配置异常采坑:Request failed with status code 401
    本地用Express作为服务端,前端Vue项目配置跨域代理,调用服务端api接口始终报错“Requestfailedwithstatuscode401”。原来发现是端口3000被占用了,被VSCode的一个插件占用了,修改为其他端口解决。具体解决过程记录一下。后端、前端配置Express服务端接口为3000,地址:http://lo......
  • Vue进阶(幺贰幺):ElementUI 表单校验注意事项
    (文章目录)一、表单检验注:prop对应的不单单是rules规则里面的验证项,同时对应着form-item下v-model绑定的值。prop绑定的类要与el-form-item下v-model的值相对应。否则就算校验元素输入字符,也会提示“请输入”之类的提示语,造成校验不友好问题。二、清除表单校验//清除表单校......