0
我遇到了讓Ajax與我的django視圖一起工作的問題。確切的錯誤是Django視圖沒有返回HttpResponse對象
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
});
});
});
所有幫助,真是不勝感激謝謝增益
。
所以,即使我要求的jQuery/AJAX沒有重新加載頁面,Django的還是需要做一回呈現模板? – crzyone9584
每個請求必須有一個有效的迴應。沒有一個不是有效的迴應。 –
感謝您的解釋和您的時間。它現在正在工作。 – crzyone9584