Reputation: 2221
I have a very basic base and child page style implementation.
My goal is to show an alert written in javascript, when i click the button in the child.html
. The problem is that when i try to use extra_js
in the child page, the javascript code does not work in the child page. However, the same js block runs when it is moved to base.html
. Did i miss something, why extra_ javascript
code is not working in the child page?
base.html
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="style.css" />
<title>{% block title %}My amazing site{% endblock %}</title>
</head>
{% comment %}
When I enable these lines, it works.Why??? it does not work in the child page???
<script>
function myfunction2(){
alert("The button is clicked in child page!");
}
</script>{% endcomment %}
<body>
<div id="sidebar">
{% block sidebar %}
<ul>
<li><a href="/">Home</a></li>
<li><a href="/blog/">Blog</a></li>
</ul>
{% endblock %}
</div>
<div id="content">
{% block content %}{% endblock %}
</div>
</body>
</html>
child.html
{% extends "base.html" %}
{% block extra_css %}
<style type="text/css">
#mydiv{
height: 50px;
width:200px;
margin-top:5px;
border:2px solid;
}
</style>
{% endblock %}
{% block extra_js %}
<script>
function myfunction2(){
alert("The button is clicked in child page!");
}
</script>
{% endblock %}
{% block content %}
<h2>Child page</h2>
<div id="mydiv">
<p>
Some text goes here...
</p>
</div>
<input type="submit" class="field button" style="float: left; " name="submit" value="Submit" id="Submit1" onclick ="myfunction2();"> </input>
{% endblock %}
Upvotes: 4
Views: 5830
Reputation: 674
You need to define an empty {% block extra_js %}
in the base.html
, then Django is placing the content of the block extra_js
of thechild.html
and place it in the block of the parent.
base.html
should look something like this:
<html>
<head></head>
<body>
...
<div id="content">
{% block content %}{% endblock content %}
</div>
{% block extra_js %}{% endblock extra_js %}
</body>
</html>
Upvotes: 9