0

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.

ny_coder_dude
  • 71
  • 1
  • 2
  • 7

3 Answers3

1

you have to load the python script that has the template library functions. Also, Why are you rendering a string into html as opposed to creating an html template? (html file with template syntax)?

wil moskal
  • 309
  • 1
  • 14
  • can you please be more specific regarding "you have to load the python script that has the template library functions"? Not sure what you mean? My actual HTML file does have template syntax - I am pulling the string from a database table that contains the body of the HTML template file, but it also contains Django Template tags. – ny_coder_dude Dec 27 '19 at 19:19
  • Within your app you should have a folder called templatetags. Within that you can have python scripts and an __init__.py file. Lets say you have a file called template_functions.py. Then within your html you have to have a line similar to the `{% load static %}` line. In the example case it would be `{% load template_functions %}` before you can actually use the functions or classes in those files. – wil moskal Dec 27 '19 at 19:31
  • Also within the `{% load template_functions %}` you have to make sure that you are registering your functions or classes to the template library – wil moskal Dec 27 '19 at 19:32
  • I'm still not sure what you mean? Please see the update to my question. If I include everything in the `html` key's value `

    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
0

The Django template engine will not render (parse) template code inside injected strings. For this to happen, you have to manually render the template code by either:

If you decide to go for the last one, you should definitely consider using the include template tag in your base.html (instead of manually rendering using render_to_string()), as it reduces the amount of manual work and is the preferred way of rendering template code inside another template.

Dabble
  • 182
  • 1
  • 3
  • 14
0

You can use file writing to do this task. Make a newfile.html in templates folder and you can do thusly

Views.py

html_tag = "{% extends \"yourapp/base.html\"%}"+"{% block content %}"
html_tag +="<p>Hello world {{ code }}</p> <script src=\"{% static \"appName/javascript_code_example.js\" %}\"></script>"
html_tag +="{% endblock content %}"
html_file = open('yourapp/templates/yourapp/newfile.html', "w")
html_file.write(html_tag)
html_file.close()
html = { 
        'code': code
        }
return render(request, r'yourapp\newfile.html', html)

In base.html

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<p>Your base code</p>
<!--The part of code you want to retrieve from views.py file-->
{% block content %}{% endblock content %}
</body>
</html>
  • However i am also curious about the thing that you have asked. That type of html code ingestion is required at many places. – Ankit Jain Nov 12 '20 at 14:54