Guest User

Untitled

a guest
Dec 12th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. #urls.py
  2. url(r'^parca_kayit/$', views.parca_kayit, name='parca_kayit'), # ajax file will call this url
  3.  
  4. # views.py
  5. def parca_kayit(request):
  6. if request.method == 'POST':
  7. pass
  8. elif request.method == 'GET':
  9. parca_turu = request.GET.get('parcaAdi') # we are getting selected item with this line. we defined 'parcaAdi' in ajax file
  10. print(parca_turu) # write selected item to the terminal
  11. form = ParcaForm()
  12. context = {'form': form}
  13. return render(request, 'tamirhane/parca_kayit.html', context=context)
  14.  
  15. ####################################################################################################
  16. # static/js/parca_kayit_ajax.js
  17. $(document).ready(function(){
  18.  
  19. $('#dropdown').on('change',function(e){
  20. e.preventDefault();
  21. var parca_turu = $(this).val();
  22. console.log("Secilen: "+parca_turu);
  23. $.ajax({
  24. url:"",
  25. method:'GET',
  26. // send selected data to the parca_kayit method which is in views.py
  27. data : {'parcaAdi' : $(this).val()}, // 'parcaAdi' will be used in request.GET.get('parcaAdi') which is in views.py, $(this).val() is selected item,
  28. success:function(gelen_parca_turu){
  29. //console.log(gelen_parca_turu);
  30. }
  31. });
  32. });
  33. });
  34. ######################################################################################################
  35. # parca_kayit.html
  36. <!DOCTYPE html>
  37. <html lang="en">
  38. <head>
  39. <meta charset="UTF-8">
  40. <title>Parca Kaydet</title>
  41. {% load static %}
  42. <link rel="stylesheet" type="text/css" href="{% static 'css/style.css' %}">
  43. <script src="{% static 'jquery/jquery-3.2.1.min.js' %}"></script>
  44. <script type="text/javascript" src="{% static 'js/parca_kayit_ajax.js' %}"></script>
  45. </head>
  46. <body>
  47. <form method="post">
  48. {% csrf_token %}
  49. <table class="tablo_parca">
  50. {{ form }}
  51. </table>
  52. <br/>
  53. <input class="submit" type="submit" value="Kaydet"/>
  54. </form>
  55. <br/>
  56. <br/>
  57. <label style="margin-left:10px;"><b>Parça Türü : </b></label>
  58. <select class="dropdown" id="dropdown" name='parcaAdi'>
  59. <option value="">Parça Türü</option>
  60. <option value="kaporta">Kaporta</option>
  61. <option value="motor">Motor</option>
  62. <option value="elektrik">Elektrik</option>
  63. <option value="lastik">Lastik</option>
  64. </select>
  65. </body>
  66. </html>
Add Comment
Please, Sign In to add comment