简体   繁体   English

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

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

I need to create an html django template a paragraph with a select dinamically created.我需要创建一个 html django 模板一个带有 select 动态创建的段落。 I read values from a txt file and I store it in a dict, when I call the render to response I pass this dict but when I try to print its values in the template it doesn't print anything.我从一个 txt 文件中读取值并将其存储在一个字典中,当我调用渲染以响应时,我传递了这个字典,但是当我尝试在模板中打印它的值时,它什么也不打印。 This is my views:这是我的看法:

views.py视图.py

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

and this is the print in the html template:这是 html 模板中的打印:

And this is the variable I'm calling in the HTML template:这是我在 HTML 模板中调用的变量:

index.html索引.html

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

Can anyone help me?谁能帮我?

try this:尝试这个:

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

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

in template:在模板中:

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

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

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