Python Django视图没有';t返回HttpResponse对象
我在让ajax使用django视图方面遇到了问题。确切的错误是Python Django视图没有';t返回HttpResponse对象,python,ajax,django,Python,Ajax,Django,我在让ajax使用django视图方面遇到了问题。确切的错误是 CustomMembers.decorators.formquestioninfo未返回 HttpResponse对象。它返回了None 该视图受以下自定义装饰器的限制 def is_god_admin(f): def wrap(request, *args, **kwargs): # This checks to see if the user is a god admin. If they are not
CustomMembers.decorators.formquestioninfo
未返回
HttpResponse
对象。它返回了None
该视图受以下自定义装饰器的限制
def is_god_admin(f):
def wrap(request, *args, **kwargs):
# This checks to see if the user is a god admin. If they are not, they get thrown to their profile page
if 'userID' not in request.session.keys() and 'username' not in request.session.keys():
return HttpResponseRedirect("/Members/Login")
else:
# lets check the roleID to what ID we need.
god_admin = Roles.objects.get(role_name='System Admin')
if request.session['roleID'] != god_admin.id:
return HttpResponseRedirect("/Members/Profile/" + request.session['userID'])
return f(request, *args, **kwargs)
wrap.__doc__ = f.__doc__
wrap.__name__ = f.__name__
return wrap
现在的视图只包含一个显示模板的返回,以及一个是否使用ajax发布请求的检查
看法
正在执行的ajax代码是:(getCookie基于django文档-
非常感谢您的帮助。谢谢您。返回f(request,*args,**kwargs)
调用decorator包装器中的view函数。但是,ajax请求的分支只打印并使函数没有返回有效响应对象的return
语句:
if request.is_ajax():
print('ajax request')
... # return a response object here to avoid returning None
因此,即使我要求jquery/ajax不要重新加载页面,django仍然需要返回来呈现模板?每个请求都必须有有效的响应。没有一个响应是无效的。感谢您的解释和时间。它现在可以工作了。@MosesKoledoye,我可以请您看看这里的相关问题吗:?
$(document).ready(function(){
$("#{{main_id}}_main_visible").click(function(e){
e.preventDefault();
var url = window.location.href;
$.ajax({
type:'get',
headers: {"X-CSRFToken": getCookie("csrftoken")},
url: url,
data: { mainid: {{main_id}} },
async: true,
cache: false
});
});
});
if request.is_ajax():
print('ajax request')
... # return a response object here to avoid returning None