简体   繁体   中英

Django: Inject a string variable from Views.py with HTML and Django template tags into HTML file

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.

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)?

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM