Answer the question
In order to leave comments, you need to log in
How to make jquery ajax request callback in django login form?
I'm making a custom django admin panel, in the authorization form everything is like this
<form method="post" class="form-signin">
{% csrf_token %}
<img class="mb-4" src="{% static 'images/icons/logo.png' %}" alt="" width="72" height="72">
<h1 class="h3 mb-3 font-weight-normal">Залогінься сучка</h1>
<label for="inputEmail" class="sr-only">Email address або username</label>
<input type="#" id="inputEmail" class="form-control username" placeholder="Email address або username" required="" autofocus="">
<label for="inputPassword" class="sr-only">Пароль</label>
<input type="password" id="inputPassword" class="form-control password" placeholder="Пароль" required="">
<div class="checkbox mb-3">
</div>
<button type="button" class="btn btn-lg btn-primary btn-block" onclick="zapros()">Увійти</button>
<p class="mt-5 mb-3 text-muted">@howareu_u</p>
</form>
function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie !== '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) === (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
function zapros() {
var username = $( ".username").val();
var parol = $( ".password").val();
var csrftoken = getCookie('csrftoken');
data = {'csrfmiddlewaretoken': csrftoken, username: username, password: parol };
$.ajax({
url: '/accounts/login/',
type: "POST",
data: data
}) ;
}
<form method = post>
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Login</button>
</form>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question