I'm calling this view (in Django) with an ajax request containing a username to query the database for. It returns a server error that the user object (in the except condition) is being used before it is defined, but it is defined in the line above!
So I thought maybe it was a scope problem, and put user = None
at the start of the elif
, knowing it would be changed in the try, but then I get 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")
}