I think this is not very related to Django more to jQuery and html behavior however what is the problem statement:
Using a more or less standard base.html template
base.html
<!DOCTYPE html>
{% load static %}
<html lang="en">
<head>
<meta charset="utf-8">
{% block stylesheets %}
<!-- load Stylesheets -->
{% endblock stylesheets %}
<title>
{% block title %}
Base Title
{% endblock title%}
</title>
</head>
<body>
<header>
{% block header %}
{% endblock %}
</header>
<main>
{% block body %}
{% endblock %}
</main>
<footer>
{% block footer %}
{% endblock %}
</footer>
{% block scripts %}
<!-- Jquery 3.5.1 CDN -->
<script src="https://code.jquery.com/jquery-3.5.1.js" integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc=" crossorigin="anonymous"></script>
<!-- Popper 1.12.9 CDN -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<!-- Bootstrap 4.0.0 CDN -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
{% endblock %}
{% block extrascripts %}
{% endblock %}
</body>
</html>
From this dashboard.html is derived by extension of base.html
{% extends 'base.html' %}
{% block title %}Dashboard{% endblock %}
{% block body %}
<div class="navbar navbar-expand justify-content-end">
some navigation bar content
</div>
some else stuff
<div class="container">
<div class="row" style="margin-top: 1em">
{% include 'dash_cards/dash_card.html' %}
</div>
</div>
some more content
{% endblock %}
{% block extrascripts %}
{% endblock %}
This works all fine and bootstrap is rendered fine - no jQuery elements are in at the moment for the better readability and easier handling the dash card is in its own file and included into dashboard.html as you can see in above snippet.
The content of dash_card.html is as follows:
<div class="card bg-darker col-md-6 border border-light">
<!-- A grey horizontal navbar that becomes vertical on small screens -->
<div class="card-body">
<canvas id="BVChart" width="75%" data-url="{% url 'url:link' %}"></canvas>
</div>
<div class="card-footer">
<-- some other fancy stuff -->
</div>
</div>
{% block extrascripts %}
<script src="https://code.jquery.com/jquery-3.5.1.js" integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/Chart.min.js"></script>
<script>
some js code to get the BVChart in the dash card canvas
</script>
{% endblock %}
Question 1: Why do I need to explicitly load jquery twice? Once in the base.html and once in the dash_card.html? If I don't do the jquery code of dash_cards.html is not executed. My approach of all those separate blocks in base.html follows the answer of this question as well as some other using {{block.super}} but non of those works for me
Just to be sure - May somebody pls confirm this - this is not an loading (time) issue as mentioned here?!
Question 2: As the current situation does not work properly (duplicate code) - jQuery must be loaded each time upwards of jquery code - What is the way forward? I just want to load it once and use it along the whole templates...
Thanks to all for your support and your input!
EDIT As requested here is the some js Code to draw the chart... I have too small hands to hold 2 hammers - the reason I asked LOL This (document.ready function) is something I also thought of first but unfortunately this does not work
var ctx = document.getElementById('BVChart').getContext('2d');
var options = $.ajax({
url: $("#BVChart").attr("data-url"),
async: false,
dataType: 'json',
}).responseJSON;
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: options.labels,
datasets: [{
label: '# of Votes',
data: options.data,
borderColor: "#fff",
borderWidth: 1
},
{
label: '# of else',
data: options.data2,
borderColor: "#999",
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {beginAtZero: true}
}]
}
}
});