function onAjaxSuccess(data){
alert("succes");}
}
function zapros() {
var username = $( ".username").val();
var parol = $( ".password").val();
var csrftoken = getCookie('csrftoken');
$.post(
"/accounts/login/",
{
csrfmiddlewaretoken : csrftoken,
username: username,
password: parol,
},
onAjaxSuccess);
function onAjaxSuccess(data){
alert("succes");}
}
<form method = post>
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Login</button>
</form>
<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>
for product in cursor.execute("SELECT * FROM products_product, products_product_media WHERE products_product.id = products_product_media.product_id").fetchall():
Forbidden (CSRF token missing or incorrect.)
не подскажите как правильно подключить csrf токен? пробую так, но django ругается <WSGIRequest: POST '/np/'>
Internal Server Error: /np/
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;
}
var csrftoken = getCookie('csrftoken');
alert (csrftoken)
$.ajax({
url: '/np/',
type: "POST",
data: name= {name: 'Monty', surname: 'Python', 'csrfmiddlewaretoken': csrftoken},
});
}
url(r'^np/$', views.np, name='np'),
$.ajax({
type: "POST",
url: url,
data: data,
success: success,
dataType: dataType
});
$.ajax({
type: "POST",
url: url,
data: data = {name: 'name', surname: surname},
success: success,
dataType: dataType
});
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'blog/media')
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)