0
我打電話給這個視圖(在Django)與包含用戶名查詢數據庫的ajax請求。它返回一個服務器錯誤,用戶對象(在除外條件中)在定義之前正在使用,但是它在上面的行中定義!服務器錯誤:用戶沒有定義之前
所以我想也許這是一個示波器的問題,並把user = None
在elif
開始,知道它會在嘗試中改變,但後來我得到None has no value DoesNotExist
。
def signup(request):
if request.method == 'GET':
return render_to_response('signup.html', context_instance=RequestContext(request))
elif request.is_ajax():
#query db for user with username provided via ajax, return if it exists
try:
user = User.objects.get(username=request.POST["username"])
except User.DoesNotExist:
return HttpResponse("false",context_instance=RequestContext(request))
else:
return HttpResponse("true", context_instance=RequestContext(request))
function usernameCheck(){
$.ajax({
type:'POST',
url:"http://omnicloud.me/signup",
data:{username: $("#username").value},
success:function(response){
if(response=="true"){
$('#passAlert').innerHTML("Sorry, that username is already taken")
}
},
headers:{"X-CSRFToken", getCookie('csrftoken')}
});
return !($('#passAlert').value == "Sorry, that username is already taken")
}
奇怪,爲什麼他抄我給了他的代碼,但改變了'User'爲'上user'線? http://stackoverflow.com/questions/8223784/sending-ajax-request-to-server-with-jquery/#8229874 –
哦,哇,我不知道這是以前的問題的一部分。 –
@ChrisPratt @ChrisPratt我原來就是這樣,它給了我一個500錯誤:'
'所以我認爲這意味着用戶名沒有找到(預期),它沒有正確處理錯誤,因爲它包括「查詢」。所以也許我的POST不包括用戶名(添加代碼) – Chris