I'm using Ajax (along with Django) to perform some action on button click. I successfully call the javascript function but I can't call the Django view. There are no errors but the print statement in my view doesn't print...?
urls.py
urlpatterns = patterns('polls.views',
url(r'^request_access/$', 'request_access',
name='request_access'),
)
views.py
def request_access(request):
print("DJANGO VIEW")
if request.method == "POST":
print("DATA: ", request.POST.get('request_data'))
return HttpResponse(
json.dumps(response_data),
content_type="application/json"
)
template.html
<button class="btn btn-green btn-sm" onclick="request_access(this)" id="{{ data }}"><i class="fa fa-plus"></i> Join Group</button>
javascript.js
function request_access($this){
console.log("button clicked");
var request_data = $this.id;
console.log("data: " + request_data);
$.post({
url: "request_access/",
data : { request_data: request_data},
success : function(json) {
$("#request-access").hide();
console.log("requested access plete");
}
})
}
I'm using Ajax (along with Django) to perform some action on button click. I successfully call the javascript function but I can't call the Django view. There are no errors but the print statement in my view doesn't print...?
urls.py
urlpatterns = patterns('polls.views',
url(r'^request_access/$', 'request_access',
name='request_access'),
)
views.py
def request_access(request):
print("DJANGO VIEW")
if request.method == "POST":
print("DATA: ", request.POST.get('request_data'))
return HttpResponse(
json.dumps(response_data),
content_type="application/json"
)
template.html
<button class="btn btn-green btn-sm" onclick="request_access(this)" id="{{ data }}"><i class="fa fa-plus"></i> Join Group</button>
javascript.js
function request_access($this){
console.log("button clicked");
var request_data = $this.id;
console.log("data: " + request_data);
$.post({
url: "request_access/",
data : { request_data: request_data},
success : function(json) {
$("#request-access").hide();
console.log("requested access plete");
}
})
}
Share
Improve this question
edited Aug 7, 2015 at 13:35
Roman C
1
asked Aug 7, 2015 at 13:28
stephsteph
7012 gold badges10 silver badges30 bronze badges
3 Answers
Reset to default 4Replace "post" with "ajax" in the code,now the print statement will work in your view.
function request_access($this){
console.log("button clicked");
var request_data = $this.id;
console.log("data: " + request_data);
$.ajax({
url: "request_access/",
data : { request_data: request_data},
success : function(json) {
$("#request-access").hide();
console.log("requested access plete");
}
})
}
Difference between $.post and $.ajax?
$.post vs $.ajax
This will return null value.
views.py
def request_access(request):
print("DJANGO VIEW")
a = request.POST.get('request_data')
print a
return HttpResponse(json.dumps(a),content_type="application/json")
js
function request_access($this){
console.log("button clicked");
var request_data = $this.id;
alert(request_data);
console.log("data: " + request_data);
$.ajax({
type: 'GET',
url: "/crm/request_access/",
data : { 'request_data': request_data},
success : function(json) {
$("#request-access").hide();
console.log("requested access plete");
}
})
}
I encountered the same problem, i.e my view no errors but my view wasn't called. Here is how I solved it :
I was trying to get the following url :
url(r'users/change_rights', 'change_user_right', name='change_user_right'),
But the users/change_rights url also matched another url I had registered :
url(r'users', 'users_table', name='users_table'),
So django was just calling the first non ajax view and not returning any errors.
To fix it add a '$' at the end of the second url so it will not contain the users/change_rights:
url(r'users$', 'users_table', name='users_table'),