Skip to content

Instantly share code, notes, and snippets.

@TimFletcher
Created May 19, 2010 20:53
Show Gist options
  • Save TimFletcher/406825 to your computer and use it in GitHub Desktop.
Save TimFletcher/406825 to your computer and use it in GitHub Desktop.
{% block scripts %}
<script type="text/javascript" charset="utf-8">
$(function(){
$('#submit').click(function(){
var username = $("input#id_username").val();
var password = $("input#id_password").val();
$form = $('#login_form');
$form.find('.errorlist').remove();
errordata = '<ul class="errorlist"><li>There was a server error processing your login. Please try again.</li></ul>'
$.ajax({
type: "POST",
url: "{% url ajax_login %}",
data: {
'username': username,
'password': password,
},
success: function(data, textStatus, XMLHttpRequest){
console.log(data)
if (data.valid == false) {
errors = '<ul class="errorlist">';
$.each(data.errors, function(key, val) {
if (key.indexOf('__all__') >= 0) {
$.each(data.errors, function(key, val) {
errors += '<li>' + val + '</li>'
});
} else {
errors += '<li><strong>' + key + ': </strong>' + val + '</li>'
}
});
errors += '</ul>';
$form.prepend(errors);
} else {
window.location.href = '{{ settings.LOGIN_REDIRECT_URL }}';
}
},
error: function(XMLHttpRequest, textStatus, errorThrown){
$(errordata).insertAfter('#dropdown h2');
}
});
return false;
});
});
</script>
{% endblock %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment