运行前端项目
进入前台项目的根目录
然后安装依赖
在前端项目根目录cmd命令框安装
npm install
启动项目
npm run serve
用户表设计
创建表的准备工作
在diango项目中的settings.py文件中配置mysql数据库相关信息
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'HOST':"localhost",
"PORT":3306, #端口号
'USER':'root',
'PASSWORD':'数据库密码',
'NAME':'数据库名'
}
}
然后在_init_文件安装数据库
import pymysql
pymysql.install_as_MySQLdb()
在diango项目子应用中的models.py文件中编写模型类
用户表: 用户名、密码、手机号
from django.db import models
# Create your models here.
class User(models.Model):
username=models.CharField(max_length=32,unique=True,verbose_name='用户名')
password=models.CharField(max_length=32,verbose_name='密码')
mobile=models.CharField(max_length=16,verbose_name='手机号')
class Meta:
verbose_name='用户表'
verbose_name_plural=verbose_name
def __str__(self):
return "%s (%s)"%(self.username,self.mobile)
生成迁移文件
python manage.py makemigrations
进行迁移
python manage.py migrate
编写视图类
from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView
from users.models import User
from django.http import HttpResponse
import re,random
# Create your views here.
# 检测用户名长度,是否重复
class CheckUsernameview(APIView):
def get(self,request,username):
if not username:
return Response({
'code':400,
'msg':'用户名错误'
})
if not (6<= len(username) <=32):
return Response({
'code': 400,
'msg': '用户长度不符合'
})
user_count = User.objects.filter(username=username).count()
if user_count:
#user_count不为0,道标数据库有这条数据
return Response({
'code':400,
'msg':'用户名已存在'
})
return Response({
'code':200,
'msg':'用户名可用'
})
# 检测手机号是否符合标准,重复
class CheckMobile(APIView):
def get(self,request,mobile):
rule=r'^1[3-9][0-9]{9}$' #定义手机号正则
rs =re.findall(rule,mobile) #返回结果是一个列表 如果匹配不到 返回的是空列表
if not rs:
return Response({
'code':400,
'msg':'手机号格式不对'
})
#程序走到这一步说明手机号格式没问题
mobile_count=User.objects.filter(mobile=mobile).count()
if mobile_count:
return Response({
'code':400,
'msg':'手机号已存在'
})
return Response({
'code':200,
'msg':'手机号可用'
})
# 检测密码是否符合标准