views.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. from django.shortcuts import render
  2. from django.http import HttpResponse,JsonResponse
  3. from django.shortcuts import render
  4. from .forms import AddForm
  5. import json
  6. import re
  7. from collections import defaultdict
  8. from .models import Scheme,specificScheme
  9. from django.core import serializers
  10. import os
  11. # Create your views here.
  12. def add(request):
  13. return HttpResponse("""
  14. <div style='color:red;'> {}</div>
  15. """.format(int(request.GET['a'])+int(request.GET['b'])))
  16. def index(request):
  17. if request.method == 'POST':
  18. form = AddForm(request.POST)
  19. if form.is_valid():
  20. a = form.cleaned_data['a']
  21. b = form.cleaned_data['b']
  22. return HttpResponse(str((int(a)+int(b))),status=404)
  23. else:
  24. form = AddForm()
  25. return render(request,'index.html',{'form':form},status=404)
  26. def ajax_list(request):
  27. a = list(range(100))
  28. return JsonResponse(a,safe = False)
  29. def ajax_dict(request):
  30. name_dict = {'twz':'love python and django','hello':'world'}
  31. return JsonResponse(name_dict)
  32. def getJson(request):
  33. """
  34. with open('./static/年级学院专业.txt','r') as f:
  35. academys = defaultdict(dict)
  36. i = 0
  37. for line in f.readlines():
  38. data = line.split('@')
  39. i+=1
  40. print(i)
  41. Scheme.objects.create(
  42. grade = data[0].strip(),
  43. academy = data[1].strip(),
  44. major = data[2].strip(),
  45. scheme_name = data[3].strip(),
  46. key = data[-1].strip()
  47. )
  48. academy = data[1]
  49. major = data[2].strip()
  50. if major not in academys[academy]:
  51. academys[academy][major] = []
  52. s = re.sub("\d+级",'',data[3].strip())
  53. s = re.sub("\d+",'',s)
  54. academys[academy][major].append(s)
  55. else:
  56. s = re.sub("\d+级",'',data[3].strip())
  57. s = re.sub("\d+",'',s)
  58. academys[academy][major].append(s)
  59. for academy_ in academys.keys():
  60. for key,item in academys[academy_].items():
  61. academys[academy_][key] = set(item)
  62. print('学院数量:',len(academys.keys()))
  63. for academy in academys.keys():
  64. print(academy,'有 {} 个专业'.format(len(academys[academy].keys())))
  65. """
  66. """
  67. files = os.listdir('./static/All')
  68. max1 = 0
  69. i = 0
  70. for file in files:
  71. with open('./static/All/'+file,'r') as f:
  72. specificScheme.objects.create(
  73. key = file.split('_')[0].strip(),
  74. content = f.read()
  75. )
  76. i+=1
  77. print(i)
  78. """
  79. grade = request.GET['class']+'级'
  80. academy = request.GET['academy']
  81. result = Scheme.objects.filter(grade=grade,academy=academy)
  82. json_data = serializers.serialize('json', result)
  83. with open('./static/All/0043f0d3922c4a238868422b8c046632_87eb47ff228a4786ad9cbd6498bc4430.json','r') as f:
  84. content = json.loads(f.read())
  85. content['check'] = json.loads(json_data)
  86. return HttpResponse(json.dumps(content))
  87. def getSchemeByKey(request):
  88. key = request.GET['key']
  89. result = specificScheme.objects.filter(key=key)
  90. json_data = serializers.serialize('json',result)
  91. res = json.loads(json_data)
  92. for i,item in enumerate(res):
  93. temp = res[i]['fields']['content']
  94. res[i]['fields']['content'] = None
  95. res[i]['fields']['content'] = json.loads(temp)
  96. return HttpResponse(json.dumps(res))