How can I fetch variables sent by django with javascript?
{% extends 'base.html' %}
{%load static%}
<link rel="stylesheet" href="{% static 'css\common_movie_tv.css' %}">
{% block content %}
<table>
<tr>
<tr>
<th>name</th>
<th>created_at</th>
<th>comment</th>
<th>evaluation</th>
</tr>
{% for c in object_list %}
<tr>
{{ c.tv.id|json_script:"tv_id" }}
<div></div>
<td>{{ c.user.nickname }}</td>
<td>{{ c.created_at }} </td>
<td>{{ c.comment }}</td>
<td><h2 class = "rate" style="--rating:{{c.stars}}">{{c.stars}}</h2></td>
</tr>
{% endfor %}
</table>
<script>
const TMDB_API_KEY = "xxxxxxxxxxx";
const tv_id = JSON.parse(document.getElementById('tv_id').textContent);
fetch(`https://api.themoviedb.org/3/tv/${tv_id}?api_key=${TMDB_API_KEY}&language=en-US`, {
method: "GET",
headers: {
"Content-Type": "application/json"
}}
)
.then(res => res.json())
.then(data => {
console.log(data)
})</script>
{% endblock %}
I want to send comment_tv.tv.id in this code to javascript.
I want to change tv_id to comment_tv.tv.id.
How I do change the code?
I want to display TV images between divs, but I can't display them even if I do console.log and I don't know if I can fetch them. What should i do?
To assign a Django context variable to a javascript variable simply do this:
let myJsVariable = "{{ my_django_variable }}";