I am beginner in django.I am working on a single text box in a django form which have number of fields.I wrote AJAX code for passing each value(entered from keyboard). How can i access this value in django class based view code. I want to save this value into a variable. Here i am trying to check given text is already existing or not. checking is done for each keyboardinput.
JavaScript
x
21
21
1
$(document).ready(function () {
2
$('#s_id > input').on('input',function(e){
3
console.log(e);
4
s_text = e.target.value;
5
$.ajax({
6
type: 'POST',
7
url: "{% url 'create-studentpage' %}",
8
data: s_text,
9
10
success: function (response) {
11
console.log("response", response);
12
13
},
14
error: function (response) {
15
console.log("error", response.responseJSON.error);
16
17
18
}
19
})
20
});
21
Advertisement
Answer
You should change the format of data you have in a json
format so that you can embed csrf_middleware_token
into the POST
:
JavaScript
1
21
21
1
$(document).ready(function () {
2
$('#s_id > input').on('input',function(e){
3
console.log(e);
4
s_text = e.target.value;
5
$.ajax({
6
type: 'POST',
7
url: "{% url 'create-studentpage' %}",
8
data: {"text": s_text, 'X-CSRFToken': {{csrf_token}} },
9
10
success: function (response) {
11
console.log("response", response);
12
13
},
14
error: function (response) {
15
console.log("error", response.responseJSON.error);
16
17
18
}
19
})
20
});
21
And then in backend, you can access it by using request.POST["text"]