简体   繁体   中英

Ajax call is not going to view function django

Hi I am sending request to a django view but its not accessing view function . Both are in same app . And I am using debugging tool so I tried to debug but on my view no call is received and My ajax call code is like this

$.ajax({
    url: '/edit_profile/',
    type: 'get', // This is the default though, you don't actually need to always mention it
    success: function(data) {
        alert(data);
    },
    failure: function(data) { 
        alert('Got an error dude');
    }
});

url.py

path('edit_profile/', views.edit_profile , name="edit_profile"),

view.py

def edit_profile(request):
       print(request)   
       print(request)     
    if request.method == 'POST':
       return render(request, 'authenticate/edit_profile.html', context)

I am debugging but call is not received in this view function .

You can see live demo here REPL

First do this if you don't want that your view check the csrf token. This can be done by using decorator @csrf_exempt.

view.py

from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def edit_profile(request):
    print(request)
    if request.method == 'GET':
        return render(request, 'authenticate/edit_profile.html')

url.py

path('edit_profile/', views.edit_profile , name="edit_profile"),

ajax request

$.ajax({
    url: '/edit_profile/',
    type: 'GET',// This is the default though, you don't actually need to always mention it
    data:{},
    success: function(data) {
        alert(data);
    },
    failure: function(data) { 
        alert('Got an error dude');
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM