middleware.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import json
  2. from django import http
  3. # def middleware(response):
  4. # def inner(request):
  5. # user_ok = '123'
  6. # password_ok = '123'
  7. # if request.method=='POST':
  8. # user = request.POST.get("user")
  9. # password = request.POST.get("password")
  10. # if user is None:
  11. # try:
  12. # user = json.loads(request.body.decode()).get("user")
  13. # except Exception:
  14. # return http.HttpResponseForbidden("请输入用户名")
  15. # if user is None:
  16. # return http.HttpResponseForbidden('请输入用户名')
  17. # if password is None:
  18. # try:
  19. # password = json.loads(request.body.decode()).get("password")
  20. # except Exception:
  21. # return http.HttpResponseForbidden("请输入密码")
  22. # if password is None:
  23. # return http.HttpResponseForbidden('请输入密码')
  24. # if user!=user_ok or password!=password_ok:
  25. # return http.HttpResponseForbidden('账号密码不正确')
  26. # res = response(request)
  27. # else:
  28. # if len(request.body.decode())==0:
  29. # return http.HttpResponseForbidden('请传入用户名和密码')
  30. # req_data = json.loads(request.body.decode())
  31. # user = req_data.get("user") # 用户名
  32. # password = req_data.get("password") # 密码
  33. # if user is None:
  34. # return http.HttpResponseForbidden('请输入用户名')
  35. # if password is None:
  36. # return http.HttpResponseForbidden('请输入密码')
  37. # if user!=user_ok or password!=password_ok:
  38. # return http.HttpResponseForbidden('账号密码不正确')
  39. # # print(request.method)
  40. # res = response(request)
  41. # return res
  42. # return inner
  43. from django.shortcuts import redirect
  44. def middleware(response):
  45. def inner(request):
  46. # print(request.path_info)
  47. # print(request.session.get('username'))
  48. if request.path_info in['/login/' ,'/logininformation/']:
  49. # print(1111111)
  50. res = response(request)
  51. return res
  52. if request.session.get('username'):
  53. print(222222)
  54. res = response(request)
  55. return res
  56. if request.session.get('username') :
  57. print((333333333))
  58. res = response(request)
  59. return res
  60. else:
  61. # return redirect('/login')
  62. # print(request.method)
  63. res = redirect('/login')
  64. return res
  65. return inner