Ajax调用不会查看django函数

Ajax调用不会查看django函数

本文介绍了Ajax调用不会查看django函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在向django视图发送请求,但它不访问视图功能.两者都在同一个应用程序中.而且我正在使用调试工具,因此尝试调试,但是在我看来,没有收到电话,我的ajax呼叫代码是这样的

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 andMy 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 .

推荐答案

您可以在此处观看实时演示 REPL

You can see live demo here REPL

如果您不希望自己的视图检查csrf令牌,请首先执行此操作.这可以通过使用装饰器@csrf_exempt来完成.

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请求

$.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');
    }
});

这篇关于Ajax调用不会查看django函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 06:53