【问题标题】:Why my code Django into HTMl variables didnt work为什么我的 Django 代码到 HTML 变量中不起作用
【发布时间】:2021-11-27 19:16:25
【问题描述】:

我正在尝试使用 python 和 django 将变量放入 html 文档中

def nombrarVariables(request):

 nombre="juan"
 doc_externo=open("SeleccionGrafica2.html")

 template=Template(doc_externo.read())
 doc_externo.close()

 contexto=Context({"variable": nombre})
 documento=template.render(contexto)

return HttpResponse(documento)

【问题讨论】:

    标签: python html django


    【解决方案1】:

    你能不能用更简洁的方式试试这个sn-p:

    from django.shortcuts import render
    
    def nombrarVariables(request):
    
        nombre = 'juan'
        context = {'nombre': nombre}
        return render(request, 'SeleccionGrafica2.html', context)
    

    在您的模板中,您可以使用{{nombre}} 访问您的变量,并在渲染函数中根据您的文件结构更改模板的路径

    【讨论】:

      猜你喜欢
      • 2015-09-28
      • 2021-12-10
      • 1970-01-01
      • 2014-01-23
      • 2013-08-06
      • 1970-01-01
      • 1970-01-01
      • 2016-01-23
      相关资源
      最近更新 更多