繁体   English   中英

在 django 中的 html 模板中传递一个变量

[英]Pass a variable in the html template in django

我需要创建一个 html django 模板一个带有 select 动态创建的段落。 我从一个 txt 文件中读取值并将其存储在一个字典中,当我调用渲染以响应时,我传递了这个字典,但是当我尝试在模板中打印它的值时,它什么也不打印。 这是我的看法:

视图.py

def index(request):
    context = {
    'variable': 'This has been sent'
    }
return render(request, 'index.html', context)

这是 html 模板中的打印:

这是我在 HTML 模板中调用的变量:

索引.html

<p>Hey Dear! {{variable}}</p>

谁能帮我?

尝试这个:

    def index(request):
        variable = 'This has been sent'
        context = {
        'variable': variable
        }

    return render(request, 'index.html', context)

在模板中:

<p>Hey Dear! {{variable}}</p>

暂无
暂无

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

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