Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #urls.py
- url(r'^parca_kayit/$', views.parca_kayit, name='parca_kayit'), # ajax file will call this url
- # views.py
- def parca_kayit(request):
- if request.method == 'POST':
- pass
- elif request.method == 'GET':
- parca_turu = request.GET.get('parcaAdi') # we are getting selected item with this line. we defined 'parcaAdi' in ajax file
- print(parca_turu) # write selected item to the terminal
- form = ParcaForm()
- context = {'form': form}
- return render(request, 'tamirhane/parca_kayit.html', context=context)
- ####################################################################################################
- # static/js/parca_kayit_ajax.js
- $(document).ready(function(){
- $('#dropdown').on('change',function(e){
- e.preventDefault();
- var parca_turu = $(this).val();
- console.log("Secilen: "+parca_turu);
- $.ajax({
- url:"",
- method:'GET',
- // send selected data to the parca_kayit method which is in views.py
- data : {'parcaAdi' : $(this).val()}, // 'parcaAdi' will be used in request.GET.get('parcaAdi') which is in views.py, $(this).val() is selected item,
- success:function(gelen_parca_turu){
- //console.log(gelen_parca_turu);
- }
- });
- });
- });
- ######################################################################################################
- # parca_kayit.html
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Parca Kaydet</title>
- {% load static %}
- <link rel="stylesheet" type="text/css" href="{% static 'css/style.css' %}">
- <script src="{% static 'jquery/jquery-3.2.1.min.js' %}"></script>
- <script type="text/javascript" src="{% static 'js/parca_kayit_ajax.js' %}"></script>
- </head>
- <body>
- <form method="post">
- {% csrf_token %}
- <table class="tablo_parca">
- {{ form }}
- </table>
- <br/>
- <input class="submit" type="submit" value="Kaydet"/>
- </form>
- <br/>
- <br/>
- <label style="margin-left:10px;"><b>Parça Türü : </b></label>
- <select class="dropdown" id="dropdown" name='parcaAdi'>
- <option value="">Parça Türü</option>
- <option value="kaporta">Kaporta</option>
- <option value="motor">Motor</option>
- <option value="elektrik">Elektrik</option>
- <option value="lastik">Lastik</option>
- </select>
- </body>
- </html>
Add Comment
Please, Sign In to add comment