[英]Django view didn't return an HttpResponse object
I'm having an issue with getting ajax to work with my django view. 我在使ajax与django视图一起工作时遇到问题。 The exact error is
确切的错误是
CustomMembers.decorators.formquestioninfo
didn't return anHttpResponse
object.CustomMembers.decorators.formquestioninfo
没有返回HttpResponse
对象。 It returnedNone
instead.它返回
None
。
The view is limited by a custom decorator which is the following. 该视图受以下自定义装饰器的限制。
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
The view right now only contains a return to show the template along with a check if ajax was used to post a request. 现在的视图仅包含显示模板的返回以及检查是否使用了ajax来发布请求。
View 视图
@is_god_admin
def formquestionsinfo(request, formid, catid, mainid):
""" Displays the forms information."""
# need the following values in both post and get methods
forms = Form.objects.all()
if request.is_ajax():
print('ajax request') # this fires then errors
else:
return render(request, formquestions.html, 'forms':forms) # this works just fine with a get request
The ajax code that is being executes is: (the getCookie is based off of django documentation - Cross Site Request Forgery protection 正在执行的ajax代码为:( getCookie基于Django文档- 跨站点请求伪造保护
$(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
});
});
});
All help is really appreciated. 非常感谢所有帮助。 Thanks gain.
谢谢。
return f(request, *args, **kwargs)
calls the view function in the wrapper of the decorator. return f(request, *args, **kwargs)
在装饰器的包装器中调用view函数。 However, the branch for ajax requests only does a print
and leaves the function without a return
statement that returns a valid response object: 但是,Ajax请求分支只能执行
print
,离开该功能没有return
, 返回一个有效的响应对象的语句:
if request.is_ajax():
print('ajax request')
... # return a response object here to avoid returning None
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.