Answers for "django csrf token in javascript"

0

django csfr token

<form action="{% url "submit-form-url-name" %}" method="post" accept-charset="utf-8">
    {% csrf_token %}
    {{ form.field1 }}
    {{ form.field2 }}
    ...
</form>
Posted by: Guest on September-20-2020
0

csrf token fetch django

let data = {
    'file': file,
    'fileName': file.name,
};
// You have to download 3rd Cookies library
// https://docs.djangoproject.com/en/dev/ref/csrf/#ajax
let csrftoken = Cookies.get('csrftoken');
let response = fetch("/upload/", {
    method: 'POST',
    body: JSON.stringify(data),
    headers: { "X-CSRFToken": csrftoken },
})
Posted by: Guest on June-26-2021
0

django csrf token in javascript

function getCookie(name) {
    let cookieValue = null;
    if (document.cookie && document.cookie !== '') {
        const cookies = document.cookie.split(';');
        for (let i = 0; i < cookies.length; i++) {
            const cookie = cookies[i].trim();
            // 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;
}
const csrftoken = getCookie('csrftoken');
Posted by: Guest on July-27-2020

Browse Popular Code Answers by Language