Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在Django Rest框架中绕过来自我的网站的Ajax请求的权限_Python_Django_Ajax_Django Rest Framework - Fatal编程技术网

Python 在Django Rest框架中绕过来自我的网站的Ajax请求的权限

Python 在Django Rest框架中绕过来自我的网站的Ajax请求的权限,python,django,ajax,django-rest-framework,Python,Django,Ajax,Django Rest Framework,我取得了API许可 def拥有_权限(自我、请求、查看): return request.user.u经过身份验证且( request.user.is_超级用户或 models.allowedouseapilist.objects.filter(user=request.user.exists)() ) 在这里,我检查请求API的用户是否被添加到AllowedouseApilist列表中。 但是我还需要从这个网站的其他页面向这个API发出AJAX请求。我如何更改允许这样做的权限? 我还将我的C

我取得了API许可

def拥有_权限(自我、请求、查看):
return request.user.u经过身份验证且(
request.user.is_超级用户或
models.allowedouseapilist.objects.filter(user=request.user.exists)()
)
在这里,我检查请求API的用户是否被添加到
AllowedouseApilist
列表中。 但是我还需要从这个网站的其他页面向这个API发出AJAX请求。我如何更改允许这样做的权限? 我还将我的CSRF令牌传递到AJAX调用中,但得到403

我这样调用Ajax:

让csrf=$(“输入[name='csrfmiddlewaretoken']”)val();
...
$.ajax({
url:“{%url”api:ajax\U api:save mark“%”,
方法:“POST”,
数据:{
“csrfmiddlewaretoken”:csrf,
“名字”:那个名字,
“价值”:价值,
},
成功:功能(数据){
...
}
})

毕竟,我的403权限被拒绝。

尝试添加一个参数,告诉视图何时允许旁路:

js:

python:

def has_permission(self, request, view):

    # check if request was triggered by ajax:
    ajax_bypass = 'bypass' in request.POST and request.POST['bypass']

    # check if use is a superuser:
    user_is_superuser = request.user.is_superuser

    # check if user is in allowed list:
    user_is_authorized = models.AllowedToUseAPIList.objects.filter(user=request.user).exists()

    return ajax_bypass or user_is_superuser or user_is_authorized

你能分享你的ajax请求和erorr本身吗?@Daniel see EditCSRF令牌有问题吗?传递正确吗?i、 e.错误是否表示csrf令牌丢失或不正确?@Daniel yep,这是正确的。问题不是通过csrf,而是让我的权限绕过Ajax调用,但对任何其他用户都是正确的。我仍然不确定我是否理解,但下面是一种绕过csrf令牌的方法。我认为这没关系,但我得到了最好的解决方案——只是关闭可浏览的API
def has_permission(self, request, view):

    # check if request was triggered by ajax:
    ajax_bypass = 'bypass' in request.POST and request.POST['bypass']

    # check if use is a superuser:
    user_is_superuser = request.user.is_superuser

    # check if user is in allowed list:
    user_is_authorized = models.AllowedToUseAPIList.objects.filter(user=request.user).exists()

    return ajax_bypass or user_is_superuser or user_is_authorized