P
P
psvm2016-09-24 22:48:46
Django
psvm, 2016-09-24 22:48:46

Django csrf. Ajax feedback form. How to send?

Good day!
Send ajax request button:

<button type="submit" class="btn btn-primary" onclick=save_feedback("{% url 'save_feedback'%}","test","test")>Submit</button>

URL:
url(r'^feedback/', views.save_feedback, name='save_feedback'),

Function to send ajax to server:
function save_feedback(url, email, message) {
    $.ajax({
        url: url,
        type: "POST",
        data: {
            'csrfmiddlewaretoken': '{{ csrf_token }}',
            'email': email,
            'message': message
        }
    });
}

view:
def save_feedback(request):
    print("debug")
    return render(request, 'base/feedback.html')

I am getting an error:
Forbidden (CSRF token missing or incorrect.): /feedback/

UPD: I apologize for not being attentive, I found the solution in the office. dock.
And I get csrf like this:
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;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2016-09-24
@psvm

https://docs.djangoproject.com/en/1.10/ref/csrf/#ajax

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://code.jquery.com/jquery-1.11.3.js"></script>
</head>
<body>

<form action="{% url form_url %}" class="js-feedback_form" method="post">
    {% csrf_token %}
    {{ form }}
    <button type="submit">Foo</button>
</form>
<script>
    // using jQuery
    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');


    var $feedback_form = $('.js-feedback_form');
    $feedback_form.submit(function (e) {
        e.preventDefault();

        $.ajax({
            method: 'post',
            url: $feedback_form.attr('action'),
            data: $feedback_form.serialize(),
            success: function(data) {
                 alert('Data send');
            }
        });
    })
    
</script>
</body>
</html>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question