设置部分
首先设置导入 框架 跨域 及子应用 注册csrf允许跨域
一首先设置导入 框架 跨域 及子应用 注册csrf允许跨域
写入白名单
// A code block
var foo = 'bar';
CORS_ORIGIN_WHITELIST = [
# 前端的源
'http://127.0.0.1:8000',
'http://localhost:8000'
]
CORS_ORIGIN_WHITELIST = [ # 配置白名单
# 前端的源
'http://127.0.0.1:8000',
'http://localhost:8000'
]
CORS_ALLOW_CREDENTIALS = True # 配置允许cookie
CORS_ALLOW_METHODS = ['*'] # 允许所有请求方式
CORS_ALLOW_HEADERS = ["*"] # 允许所有请求头
在DATABASES里设置数据库
使用自定义模型类来重新定义模型类
// A code block
var foo = 'bar';
// An highlighted block
AUTH_USER_MODEL = 'user.User'
二其次安装数据库
路由分发
编写视图
// A code block
var foo = 'bar';
// An highlighted block
from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
import re
from .models import User
# Create your views here.
class CheckUsername(APIView):
def get(self, request, username):
# 判断用户格式
if re.findall(r'^[a-zA-Z]\W{4,15}$', username):
return Response({"code": 404, 'msg': "用户名格式不正确"})
# 判断用户是否重复
if User.objects.filter(username=username):
return Response({"code": 400, 'msg': "用户重复"})
else:
return Response({"code": 200, 'msg': "OK"})
class CheckMobile(APIView):
def get(self,request,mobile):
if re.findall(r'^1[3-9]\d{9}$', mobile):
return Response({"code": 404, 'msg': "用户名格式不正确"})
if User.objects.filter(mobile=mobile):
return Response({"code": 400, 'msg': "手机号重复"})
else:
return Response({"code": 200, 'msg': "OK"})