python - Django post on click with jquery not getting a response -


i trying setup button on website when clicked new model item saved database. segments of code pasted below , discuss problem after.

textbook.html

<script src="../../static/textchange/wishlisting.js"></script> 

...

<input type="button" id="addwishlist" value="add wishlist"></input> 

wishlisting.js

$('#addwishlist').click(function() { $.ajax({     url: '/textbook/',     method: 'post', // or (get), whatever need     data: {         name: request.user,         book: text, // data need pass function         click: true     }     success: function (data) {         // success callback         // can process data returned function views.py     } }); 

});

views.py

def textbook(request, uisbn):     form4 = addwishlist(request.post or none)     ltextbook = textbook.objects.filter(isbn = uisbn)     text = ltextbook[0]     wishlists = wishlist.objects.filter(textbook = text)     listings = posting.objects.filter(textbook = text)     if request.post.get('click', false):         new = wishlist(textbook = text, user = name, wish_date = datetime.now())         new.save() 

from understanding when click "add wishlist" button should correlate id id of jquery referenced in wishlisting.html , view should loaded , database item should made. missing in thought process?

thanks.

some changes begin:

1.) use staticfiles load files in templates:

{% load staticfiles %}  # might need change path <script type="text/javascript" src="{% static 'textchange/wishlisting.js' %}"></script> 

2.) used camelcase:

<input type="button" id="addwishlist" value="add wishlist" uisbn="set_value_here"></input> 

to answer question:

views.py:

from django.http import jsonresponse django.shortcuts import get_object_or_404 django.views.decorators.http import require_http_methods  datetime import datetime   @require_http_methods(['post']) def textbook(request):     uisbn = request.post.get('uisbn')     # i'm assuming of correct     ltextbook = get_object_or_404(textbook, isbn=uisbn)     if ltextbook:         text = ltextbook[0]         wishlists = wishlist.objects.filter(textbook=text)         listings = posting.objects.filter(textbook=text)         new = wishlist(textbook=text, user=request.user, wish_date=datetime.now())         new.save()         new_created = true     else:         new_created = false      data = {         'new_created': new_created     }     return jsonresponse(data) 

html:

$('#addwishlist').click(function(e){     e.preventdefault();     $.ajax({         type: "post",         // use name of url not '/textbook/'         url: "{% url 'textbook' %}",         data: {             "uisbn": $(this).attr("uisbn"),             csrfmiddlewaretoken: "{{ csrf_token }}",         },         datatype: "json",         success: function(data) {             if (data.new_created) {                 // here             } else {                 // else here             }         },         error: function (rs, e) {             alert('sorry, there error.');         }     }); }); 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -