The first thing I want to do is profusely thank any person that takes the time to read this. The post looks long, but this is mostly because I formatted it with bullet points and I wanted to be as detailed as possible and provide a minimal working example. This is my first time embarking on an independent coding project and my first ever Stack Exchange post so even though I checked to not break any rules, I might have missed something.
I am working on my first django project (supposed to be a simple blog) and I think I am running into a lot of unknown unknowns. I want to: - Render [;\LaTeX] style mathematical formulae in an article template that I am using. The template is an HTML file and the source code is found here. It extends this base template
I have tried
At this point, I got desperate. I then tried:
I'll risk expanding on one aspect, that of template files. It's perhaps a shame that Django doesn't come with a common base template, that one has to actively replace should it not fulfill one's needs. Sooner or later one will need one, and it would be nice if installing Django gave you a canonical one and all the beginner tutorials used it.
Anyway, taking Kent Shikama's answer, my site's template for generating his html would look like
{% extends "myproject/base.html" %}
{% block extrascripts %}
<script type="text/javascript" id="MathJax-script" async
src="https://cdn.jsdelivr.net/npm/[email protected]/es5/tex-mml-chtml.js">
</script>
{% endblock extrascripts %}
{% block content %}
$$x=\frac{-b+\sqrt{b^2-4ac}}{2a}$$
{% endblock content %}
As for what that base.html might be, a simple one that would yield the the html in that answer plus a couple of HTML comments, would be
<html>
<head>
{% block scripts %}
<!-- put here any scripts (such as JQuery ) that you want to be loaded
UNLESS the specific template requires otherwise
by overriding this block. Add scripts via extrascripts below.
-->
{% endblock scripts}
{% block extrascripts %}{%endblock extrascripts%}
{% block css %}
<!-- put here any CSS styling you want to be loaded
UNLESS the specific template requires otherwise,
by overriding this block. Add extra styling via extracss below.
-->
{% endblock css %}
{% block extracss %}{% endblock extracss %}
</head>
<body>
{% block content %}{% endblock content %}
</body>
</html>
If it's a maths blog project, you might well promote the MathJax script from this individual template, into block scripts
in base.html. Your templates would then contain only a content
block and could freely use MathJax.
The key thing is that your specific page templates only need to include scripts and styling that are different from the scripts and styling that you use with every page on your project. You can then re-style your entire site just be altering that base html.
In the real world an intermediate level of templates that extend base.html and are in turn extended by individual page templates is common, so an entire class of pages can be specified without repetition.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With