用户注册后端接口

  • models.py

    from django.db import models
    # 用户表
    class User(models.Model):
      name = models.CharField(max_length=32)
      password = models.CharField(max_length=255)
    
  • urls.py

    from django.urls import path
    from . import views
    urlpatterns = [
        path('register/',views.UserRegisterView.as_view()),
    ]
    
  • views.py

    from rest_framework.views import APIView
    from rest_framework.response import Response
    
    # 用户登录
    class UserRegisterView(APIView):
      # post方式提交
      def post(self,request):
        # 获取前端传来的数据
        data = request.data
        # 判断用户名是否唯一
        user = models.User.objects.filter(name=data.get('name','')).first()
        if user:
          # 用户名已存在
          return Response({
            "status":203,
            "msg":"用户名已存在"
          })
        # 验证用户名和密码是否正确
        obj = models.User.objects.create(**data)
        # 判断
        if obj:
          # 成功返回
          return Response({
            "status":200,
            "msg":"注册成功"
          })
        # 失败返回
        return return Response({
            "status":201,
              "msg":"注册失败"
          })
    

results matching ""

    No results matching ""