1

Having a hard time deleting an object using jquery/ajax. I have implemented a table that has rows containing a list of objects from Appointment model. There is a delete button in each row.

Template for objects' table is :

<table class="table table-striped table-bordered table-list">
    <thead>
     <tr>
        <th><em class="fa fa-cog"></em></th>
        <th class="hidden-xs">ID</th>
        <th>Patient Name</th>
        <th>Day</th>
        <th>Time</th>
        <th>Location</th>
     </tr>
    </thead>
    <tbody>
        {% for appointment in appointments %}
            <tr>
                <td align="center">
                    <button class="delete_button" id="{{ appointment.id }}">
                        <em class="fa fa-trash"></em>
                    </button>
                </td>
                  <td>1</td>
                <td class="hidden" id="appointment_id">{{ appointment.id }}</td>
                <td>{{ appointment.patient }}</td>
                <td>{{ appointment.day }}</td>
                <td>{{ appointment.time }}</td>
                <td>{% if appointment.clinic %}
                      {{ appointment.clinic }}
                      {% endif %}
                      {% if appointment.hospital %}
                      {{ appointment.hospital }}
                      {% endif %}
                </td>
            </tr>
        {% endfor %}
    </tbody>
</table>

Javascript is as below :

$(document).ready(function(){
    $(".delete_button").click(function() {
        var id = $(this).attr('id');
        $.ajax({
            url: "{% url 'deleteappointment' %}",
            data: { 'id' : id },
            beforeSend: function(xhr) {
                xhr.setRequestHeader("X-CSRFToken", {% csrf_token %} );
            },
            success: function(response){
            }
        });
    });
});

Nothing is happening when I click on the delete button. What am I doing wrong here ?

EDIT 2 This is going on in ajax request : Syntaxerror in console :

beforeSend: function(xhr) {
      xhr.setRequestHeader("X-CSRFToken", <input type='hidden' name='csrfmiddlewaretoken' value='LUgmNbcWfkIR9KpLi7lzsn0QOk' /> );
                },

How do I prevent csrftoken tag to render the whole input field here ?

4
  • 1
    any error in console?? Commented Nov 29, 2016 at 9:00
  • I'd imagine its because you don't declare its a post request but hard to say without more info, if it is then use $.post instead of $.ajax Commented Nov 29, 2016 at 9:00
  • @Manu uncaughtsyntax error in csrf token header. django's {% csrf_token %} is giving an input field in the header. Commented Nov 29, 2016 at 9:07
  • Found Error in header : The header is looking like this : beforeSend: function(xhr) { xhr.setRequestHeader("X-CSRFToken", <input type='hidden' name='csrfmiddlewaretoken' value='LUgmNbcWfkIR9KpLi7lzsn0QOkREPZmxABWf' /> ); }, Commented Nov 29, 2016 at 9:12

1 Answer 1

6

You're using the {% csrf_token %} template tag which inserts the token in a html format for a form. You can use the other type of brackets (inside quotes)

xhr.setRequestHeader("X-CSRFToken", {% csrf_token %} );

should be

xhr.setRequestHeader("X-CSRFToken", "{{ csrf_token }}");
Sign up to request clarification or add additional context in comments.

3 Comments

Already did this. Now getting this : "Uncaught ReferenceError : l3ts9IEg7JfopQmeb0xiGq5AgPoHFVLjaK.. is not defined." The random string is the csrf token.
Did you forget the quotes around the tag?... Although this is verging into a separate issue..
Replacing {% csrf_token %} with "{{ csrf_token }}" worked like a charm!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.