In one of my templates, I have a for loop that goes over all the items. When a person likes or dislikes an item, I want to handle that with my function. Setting the button's HTML to something like this works:
<button onclick='update_like(arg1, arg2)'></button>{{ item.id}}
However, I need to pass my template variables to the function. So I tried something like this:
<button onclick='update_like({{item.id}}, {{item.name}})'></button>{{ item.name}}
But clicking the button just ouputs: Uncaught SyntaxError: Unexpected token 'default'
This is a trimmed down version of the full template I'm using:
{% extends 'base.html' %}
{% load static %}
{% block content %}
<div class="list">
{% for item in items %}
<button onclick='update_like({{item.id}}, {{item.name}})'></button>{{ item.name}}
{% endfor %}
</div>
<script>
var count = {};
function update_like(item_id, item_name) {
count[item_id] = 1;
console.log(count);
return;
}
</script>
{% endblock %}