Advertisement
dregueiro

ajax error

Feb 11th, 2020
503
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.95 KB | None | 0 0
  1. template
  2.  
  3. {% extends 'base/base.html' %}
  4.  
  5. {% block content %}
  6.  
  7. <form class="user" method="POST">
  8. <div class="card mb-4 py-3 border-left-primary">
  9. <h2 class="m-2 font-weight-bold text-primary"> Pre Testing Details</h2>
  10. <div class="dropdown-divider"></div>
  11. <div class="card-body">
  12. {% csrf_token %}
  13. <div class="row form-group">
  14. <div class="col-sm-6">
  15. <label id="id_school" class="text-body ">School</label>
  16. {{ form_enc.school }}
  17. </div>
  18. <div class="col-sm-3">
  19. <label class="text-body ">Testing Date</label>
  20. {{ form_enc.fecha_examen }}
  21. </div>
  22. <div class="col-sm-3">
  23. <label class="text-body ">Graduation Date</label>
  24. {{ form_enc.fecha_graduacion }}
  25. </div>
  26. </div>
  27. <div class="dropdown-divider"></div>
  28. <div class="row form-group">
  29. <button type="submit" class="btn btn-success waves-effect waves-float mr-2"><span class="fa fa-save"></span> Save</button>
  30. <a href="{% url 'testing:pretest_list' %}" class="btn btn-danger waves-effect waves-float"><span class="fa fa-undo"></span> Cancel</a>
  31. </div>
  32. <h4>Select Candidates</h1>
  33. <div class="table-responsive">
  34. <table width="100%" class="table table-bordered " id="dataTables-example" cellspacing="0">
  35. <thead>
  36. <tr>
  37. <th>Id</th>
  38. <th>Student Name</th>
  39. <th>Rank</th>
  40. <th>Test</th>
  41. <th>Midterm</th>
  42. <th>Paid</th>
  43. <th>Order Belt</th>
  44. </tr>
  45. </thead>
  46. <tbody>
  47. {% for item in student %}
  48. <tr id="{{ item.id }}">
  49. <td>{{ item.id }}</td>
  50. <td>{{ item.first_name }}, {{ item.first_last_name }}</td>
  51. <td>{{ item.rank }}</td>
  52. <td><input type="checkbox" name="test" id="test" /> </td>
  53. <td><input type="checkbox" name="midterm" id="midterm" /> </td>
  54. <td><input type="checkbox" name="pago" id="pago"/> </td>
  55. <td><input type="checkbox" name="orderbelt" id="orderbelt" /> </td>
  56. </tr>
  57. {% endfor %}
  58. </tbody>
  59. </table>
  60. </div>
  61. </div>
  62. <!-- botones aqui -->
  63. </div>
  64. </form>
  65. {% endblock %}
  66.  
  67.  
  68. {% block js_page %}
  69. <script>
  70. $(function () {
  71.  
  72. $("#id_fecha_examen").datetimepicker({
  73. format: 'd/m/Y',
  74. timepicker:false
  75. });
  76. $("#id_fecha_graduacion").datetimepicker({
  77. format: 'd/m/Y',
  78. timepicker:false
  79. });
  80.  
  81. $("#school").focus();
  82. });
  83.  
  84. var token = $('input[name=csrfmiddlewaretoken]').val();
  85. $.ajaxSetup({
  86. beforeSend: function(xhr, settings) {
  87. if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
  88. xhr.setRequestHeader("X-CSRFToken", token);
  89. }
  90. }
  91. });
  92.  
  93.  
  94. $('form').on('submit', function(e) {
  95. e.preventDefault();
  96. var $dataElements = $('#dataTables-example').find('td'),
  97. data = [];
  98.  
  99. $.each($dataElements, function(i, elem){
  100. data.push($(elem).html());
  101. });
  102. var template="{% url 'testing:pretest_list' %}";
  103. $.ajax({
  104. type: "POST",
  105. url: template,
  106. data: {data:JSON.stringify(data)},
  107. beforeSend: function(xhr,settings){
  108. $.ajaxSettings.beforeSend(xhr,settings);
  109. },
  110. success: function(response){
  111. console.log(response);
  112. cerrar_modal();
  113. window.location.reload()
  114. //alert(response);
  115. var tipo=(response.startsWith('Escuela no existe') ? 'red':'green' );
  116. mensaje(response,tipo);
  117. },
  118. error: function(jqXHR, textStatus, errorThrow){
  119. console.log(textStatus, errorThrow);
  120. mensaje(errorThrow,'red');
  121. cerrar_modal();
  122. }
  123. });
  124.  
  125. });
  126.  
  127. </script>
  128. {% endblock js_page %}
  129.  
  130.  
  131.  
  132.  
  133. VIEW
  134.  
  135. def pretest(request, pretest_id=None):
  136. template_name="testing/pretesting_form.html"
  137. rango = ProgramaRank.objects.filter(activo=True).order_by('pk')
  138. form_pretest ={}
  139. contexto = {}
  140.  
  141. if request.method=="GET":
  142. form_pretest = PreTestEncForm()
  143. enc= PreTestEnc.objects.filter(school_id=pretest_id).first()
  144. if enc:
  145. det=PreTestDet.objects.filter(pretestenc = enc)
  146. student = People.objects.filter(school_id=enc.school_id)
  147. if enc.fecha_examen:
  148. fecha_examen = datetime.date.isoformat(enc.fecha_examen)
  149. else:
  150. fecha_examen = 0
  151. if enc.fecha_graduacion:
  152. fecha_graduacion = datetime.date.isoformat(enc.fecha_graduacion)
  153. else:
  154. fecha_graduacion = 0
  155. e={
  156. 'school': enc.school,
  157. 'fecha_examen': fecha_examen,
  158. 'fecha_graduacion': fecha_graduacion,
  159. }
  160. form_pretest= PreTestEncForm(e)
  161. else:
  162. det = None
  163. student = People.objects.all()
  164.  
  165. contexto = {'rango':rango, 'encabezado':enc, 'detalle':det,'student':student, 'form_enc':form_pretest}
  166.  
  167. if request.method=="POST":
  168. school = request.POST.get("school")
  169. fecha_examen = request.POST.get("fecha_examen")
  170. # fecha_examen = datetime.datetime.strptime(fecha_examen, "%d/%m/%Y")
  171. fecha_graduacion = request.POST.get("fecha_graduacion")
  172. # fecha_graduacion = datetime.datetime.strptime(fecha_graduacion, "%d/%m/%Y")
  173. table = request.POST.get("data")
  174.  
  175.  
  176. if not pretest_id:
  177. enc = PreTestEnc(
  178. school = school,
  179. fecha_examen = fecha_examen,
  180. fecha_graduacion = fecha_graduacion
  181. )
  182. if enc:
  183. enc.save()
  184. pretest_id = enc.id
  185.  
  186. else:
  187. school = School.objects.filter(pk = school).first()
  188. enc= PreTestEnc.objects.filter(pk=pretest_id).first()
  189. if enc:
  190. enc.school = school
  191. enc.fecha_examen = fecha_examen
  192. enc.fecha_graduacion = fecha_graduacion
  193. enc.save()
  194.  
  195. if not pretest_id:
  196. return redirect("testing:pretest_list")
  197.  
  198. for x in table:
  199. people = request.POST.get("id_student")
  200. rank = request.POST.get("student.rank")
  201. test = request.POST.get("test")
  202. midterm = request.POST.get("midterm")
  203. paid = request.POST.get("paid")
  204. orderbelt = request.POST.get("orderbelt")
  205. det = PreTestDet(
  206. pretestenc = enc,
  207. people = people,
  208. rangoactual = rank,
  209. test = test,
  210. midterm = midterm,
  211. pago = paid,
  212. order_belt = orderbelt
  213. )
  214. # if det:
  215. #det.save()
  216.  
  217. return redirect("testing:pretest_list")
  218.  
  219. return render(request, template_name, contexto)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement