简体   繁体   English

Django:将带有 HTML 和 Django 模板标签的 Views.py 中的字符串变量注入到 HTML 文件中

[英]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.所以这是我的问题:我有一个包含 HTML 和Django 模板标签的 Python 字符串,并希望在我转到该页面时将其注入到基本 HTML 文件中。 Although, when I go to the page, all the HTML renders, but the Django Template Tags do not, and are treated literally as strings?虽然,当我进入页面时,所有的 HTML 都会呈现,但 Django 模板标签却没有,并且从字面上被视为字符串?

Here is a simplified example of the issue:这是该问题的一个简化示例:

Views.py视图.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.当我使用python3 manage.py runserver在本地机器上运行应用程序并转到呈现 base.html 的 URL 时,我将看到的所有内容是Hello world {{ code }} ,并且不执行 Javascript 代码。 Instead of {{ code }} I'd like to see the actual value of the 'code' key in the html dictionary in the Views.py file.我想查看 Views.py 文件中 html 字典中'code'键的实际值,而不是{{ code }}

If my base.html file is as follows:如果我的base.html文件如下:

{% 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.然后将启用 Javascript,我将在屏幕上看到Hello world value_of_code_variable

you have to load the python script that has the template library functions.您必须加载具有模板库函数的python 脚本。 Also, Why are you rendering a string into html as opposed to creating an html template?另外,为什么要将字符串渲染为 html 而不是创建 html 模板? (html file with template syntax)? (带有模板语法的 html 文件)?

The Django template engine will not render (parse) template code inside injected strings. Django 模板引擎不会在注入的字符串中呈现(解析)模板代码。 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.如果您决定使用最后一个,您绝对应该考虑base.html使用include模板标记(而不是使用render_to_string()手动渲染),因为它减少了手动工作量并且是首选的渲染方式另一个模板中的模板代码。

You can use file writing to do this task.您可以使用文件写入来完成此任务。 Make a newfile.html in templates folder and you can do thusly在模板文件夹中创建一个 newfile.html ,你可以这样做

Views.py视图.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在 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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