So this is my issue: I have a Python string that contains both HTML and Django Template Tags and want to inject it into a base HTML file when I go to that page. Although, when I go to the page, all the HTML renders, but the Django Template Tags do not, and are treated literally as strings?
Here is a simplified example of the issue:
Views.py
def page(request, code):
html = {
'code': code
'html': """<p>Hello world {{ code }}</p> <script src="{% static 'appName/javascript_code_example.js' %}"></script>"""
}
return render(request, 'base.html', html)
base.html
{% load static %}
...
{{ html | safe }}
...
And all I will see when I run the app on my local machine with python3 manage.py runserver
and go to the URL that renders base.html is Hello world {{ code }}
, and the Javascript code is not executed. Instead of {{ code }}
I'd like to see the actual value of the 'code'
key in the html dictionary in the Views.py file.
If my base.html file is as follows:
{% load static %}
...
<p>Hello world {{ code }}</p>
<script src="{% static 'appName/javascript_code_example.js' %}"></script>
...
Then the Javascript will be enabled and I will see Hello world value_of_code_variable
on the screen.
Hello world {{ code }}
` in the actual base.html file, then everything works as expected. When I try to inject it as a string, it treats the `{{ code }}` and `{% static 'appName/javascript_code_example.js' %}` as literal strings - this is the issue. – ny_coder_dude Dec 27 '19 at 19:47