08 实现登录功能

  • 实现登录功能
    •  1 # bysms.urls.py
       2 from django.contrib import admin
       3 from django.urls import path, include
       4 # 静态文件服务声明
       5 from django.conf.urls.static import static
       6 
       7 urlpatterns = [
       8                   path('admin/', admin.site.urls),
       9                   # 全路由
      10                   # path('sales/orders/', list_orders),
      11                   # 路由子表
      12                   path('sales/', include('sales.urls')),
      13                   path('api/mgr/', include('mgr.urls'))
      14               ] + static('/', document_root='./z_dist')
    • 1 # mgr.urls.py
      2 from django.urls import path
      3 from mgr import customer, sign_in_out
      4 
      5 urlpatterns = [
      6     path('customers', customer.dispatcher),
      7     path('signin', sign_in_out.signin),
      8     path('signout', sign_in_out.signout),
      9 ]
    •  1 # mgr.sign_in_out.py
       2 from django.http import JsonResponse
       3 from django.contrib.auth import authenticate, login, logout
       4 
       5 
       6 # 登录处理
       7 def signin(request):
       8     # 从HTTP POST 请求中获取用户名、密码参数
       9     userName = request.POST.get('username')
      10     passWord = request.POST.get('password')
      11 
      12     # 使用django auth 库里面的方法校验用户名密码
      13     user = authenticate(username=userName, password=passWord)
      14 
      15     # 如果能找到用户,并且密码正确
      16     if user is not None:
      17         if user.is_active:
      18             if user.is_superuser:
      19                 # 登录
      20                 login(request, user)
      21                 # 在session中存入用户类型
      22                 request.session['usertype'] = 'mgr'
      23                 print('xxxxxxxxxxxxxxxxxxxx')
      24                 return JsonResponse({'ret': 0})
      25             else:
      26                 return JsonResponse({'ret': 1, 'msg': '请使用管理员账号登录'})
      27         else:
      28             return JsonResponse({'ret': 0, 'msg': '用户已经被禁用'})
      29     else:
      30         # 否则提示用户名或者密码错误
      31         return JsonResponse({'ret': 1, 'msg': '用户名或者密码错误'})
      32 
      33 
      34 def signout(request):
      35     # 使用登出方法
      36     login(request)
      37     return JsonResponse({'ret': 0})

       

  • 测试我们的代码
    •  1 # tc_login.py
       2 import requests, pprint
       3 
       4 payload = {
       5     'username': 'byhy',
       6     'password': 'lzp123456'
       7 }
       8 
       9 response = requests.post('http://localhost/api/mgr/signin', data=payload)
      10 
      11 pprint.pprint(response.json())
    •  1 # tc_addcustomer.py
       2 import requests, pprint
       3 
       4 data = {
       5     "action": "add_customer",
       6     "data": {
       7         "name": "武汉市桥西医院",
       8         "phonenumber": "13345679934",
       9         "address": "武汉市桥西医院北路"
      10     }
      11 }
      12 
      13 response = requests.post('http://localhost/api/mgr/customers', json=data)
      14 
      15 pprint.pprint(response.json())
    •  1 # tc_deletecustomer.py
       2 import requests, pprint
       3 
       4 data = {
       5     "action": "del_customer",
       6     "id": 6
       7 }
       8 
       9 response = requests.delete('http://localhost/api/mgr/customers', json=data)
      10 
      11 pprint.pprint(response.json())
    •  1 # tc_modifycustomer.py
       2 import requests, pprint
       3 
       4 data = {
       5     "action": "modify_customer",
       6     "id": 6,
       7     "newdata": {
       8         "name": "武汉市桥北医院",
       9         "phonenumber": "13345678888",
      10         "address": "武汉市桥北医院北路"
      11     }
      12 }
      13 
      14 response = requests.put('http://localhost/api/mgr/customers', json=data)
      15 
      16 pprint.pprint(response.json())
    •  1 # tc_listcustomer.py
       2 import requests, pprint
       3 
       4 params = {
       5     'action': 'list_customer',
       6 }
       7 
       8 response = requests.get('http://localhost/api/mgr/customers', params=params)
       9 
      10 pprint.pprint(response.json())
posted @ 2025-12-07 00:25  理想赵雷  阅读(4)  评论(0)    收藏  举报