【发布时间】:2019-08-22 09:07:02
【问题描述】:
我开发了一个 django 应用程序,其中有一个 d3js 交互式树。我使用 d3.json 从 django 视图中获取树数据。我创建了一个装饰器来检查用户是否已登录以允许该请求。当用户登录时我没有问题,但是当装饰器返回带有重定向 url 的 jsonResponse 时,我只有带有状态描述的状态错误。
我阅读了 d3js 和 promise 文档,但我没有找到一个答案来返回带有我的自定义响应的 jsonresponse。
d3.json(d.data.url).then(function(data) {
// process data no problem
}, function(error){
console.log(error);
});
def check_user_permission_js(view_function):
@wraps(view_function)
def wrapper(request, *args, **kwargs):
if request.user.is_authenticated:
return view_function(request, *args, **kwargs)
messages.warning(request,
"Your account doesn't have access to this page "
+ "or your session has expired. "
+ "To proceed, please login with an account that has access.")
return JsonResponse({'not_authenticated': True,
'redirect_url': settings.LOGIN_URL,'data':[]}, status=500)
return wrapper
【问题讨论】: