Search code examples
djangodjango-templatesuuid

UUID value as function argument - Uncaught SyntaxError


I have a function which takes an "id" as argument:

<script>
    function deleteBl(id){
        fetch('', {
            method: 'DELETE',
            headers: {
                'X-CSRFToken': '{{ csrf_token }}'
            },
            body: JSON.stringify({
                'id': id
            }),
            credentials: 'same-origin',
        })
    }

</script>

and below Django template:

    {% for item in bl_query_set %}
    <tr>
        <th scope="row">{{ forloop.counter }}</th>
        <td>{{ item.bl_number }}</td>
        <td class="text-center">{{ item.cargo_name }}</td>
        <td>{{ item.cargo_quantity }} {{ item.cargo_measurement }}</td>
        <td>
            <a onclick="deleteBl({{ item.id }})">
                <i class="fas fa-trash-alt"></i>
            </a>
        </td>
    </tr>
    {% endfor %}

There are no any console errors if I use default Id as primary key. But I need to use UUID as a primary key and in that case I get an error: "Uncaught SyntaxError: Invalid or unexpected token":

enter image description here

How I can resolve mentioned issue?


Solution

  • You need to pass it as a string:

    <a onclick="deleteBl('{{ item.id }}')">