The first step is to introduce jquery files, either online or offline, to add to your own staticfiles
What's demonstrated here is adding offline
<script src={% static 'js/jquery-2.1.1.min.js' %}></script>
Note that this sentence is added before the ajax jquery script you write.
Then the HTML form is as follows:
'django.middleware.csrf.CsrfViewMiddleware',
You need to add this sentence to your jquery.
Then the whole ajax script, the comparison written here is simply to highlight the communication with django:$.ajaxSetup({ data: {csrfmiddlewaretoken: '{{ csrf_token }}' }, });
Next, write about our backendView.pyBackend handler in
Because there is Chinese soViews.py Begin withdef comments_upload(request): if request.method == 'POST': print "it's a test" #For testing print request.POST['input'] #Test if you can receive an input field from the front end return HttpResponse(<span style="line-height: 1.42857;">request.POST['input']</span><span style="line-height: 1.42857;">) #Last Return to Front End </span> else: return HttpResponse("<h1>test</h1>")
Note: The url page of ajax POST is not the page that receives input.....I've slaughtered this place a second time...That is comments_The upload function is the processing function for the page of the POST request, and the page we submit the request (HTML above) is only the page that provides the input and receives the processing result, so the receive processing statement cannot be written in the HTML above, it needs to be written to comments_The page where the upload function is located.#coding=utf8