【发布时间】:2017-03-28 09:33:29
【问题描述】:
我在让 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, 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 来发布请求。
查看
@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
正在执行的 ajax 代码是:(getCookie 基于 django 文档 - Cross Site Request Forgery protection
$(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
});
});
});
非常感谢所有帮助。感谢收获。
【问题讨论】: