Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed a simple working ajax example for django forms
    primarykey
    data
    text
    <p>Basically I need something similar to <a href="http://www.w3schools.com/jquery/jquery_ajax_get_post.asp">http://www.w3schools.com/jquery/jquery_ajax_get_post.asp</a> done in django. I've downloaded the samples and tested it locally with a localhost + php and it works out fine but I can't seem to get it to work in django no matter how simple the example is. Here's basically what I've done based on the example from the link above with slight modification</p> <p>the javascript:</p> <pre><code>&lt;script type="text/javascript"&gt; $(document).ready(function(){ $("#my_form").submit(function(){ $.post("", {name:"Donald Duck", city:"Duckburg"}, function(data,status){ alert("Data: " + data + "\nStatus: " + status); }) .fail(function() { alert("error"); }); return false; }); }); &lt;/script&gt; </code></pre> <p>the urls:</p> <pre><code>url(r'^ajax/$', views.ajax_test, name="ajax"), </code></pre> <p>the views:</p> <pre><code>def ajax_test(request): if request.method == 'POST' and request.is_ajax(): name = request.POST['name'] city = request.POST['city'] message = name + ' lives in ' + city return HttpResponse(json.dumps({'message': message})) #tried without the json. Doesn't work either return render(request, 'books/ajaxTest.html') </code></pre> <p>the html:</p> <pre><code>&lt;form id="my_form" action="" method="post" {% if form.is_multipart %}enctype="multipart/form-data"{% endif %}&gt;{% csrf_token %} &lt;input type="submit" value="Send"&gt; &lt;/form&gt; </code></pre> <p>The form is suppose to include a django form but since I can't even get the basics to work, that would be kinda pointless. Someone mentioned about the csrf_token tag but removing that doesn't solve the problem either. The output of the above example basically just yields the alert('error') and nothing else. I've been through so many examples but I can't even get the most basic ones to work</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload