I'm going to get parameter form AJAX request in Django, Here's what I'm doing:
base.html:
{% csrf_token %}First name:
main.js:
$(document).ready(function(){
$("#register").live("click", function(e){
$.post("/", {
name: "MY TEXT",
});
});
});
views.py:
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.core.context_processors import csrf
def home(request):
if request.method == 'POST':
print request.POST['name']
return render_to_response('registration.html', {},
context_instance=RequestContext(request))
Yes, I know that at this moment my JS doesnt get real data from text form, it sends just a static text "MY TEXT". but when I press button, I get
"MultiValueDictKeyError at /
"Key 'name' not found in ""
What I'm doing wrong?
I've changed my code:
main.js
$(document).ready(function(){
$("#register").live("click", function(e){
e.preventDefault();
$.post("/", {
name:'MY TEXT'
});
});
});
base.html:
{% csrf_token %}First name:
It works, thanks!
Although, I have two questions:
Where am I sending 'MY TEXT' in this case? I ment it returns the ACTUAL data from name field and doesn't return "MY TEXT"
The page is still reloading. And I wanted to make it completely AJAX. I mean create a python function to add data from POST request to MySQL database, and return to AJAX-script the result of the operation. And everything without page reload. How can I do this?