Почему мой код Django в переменных HTMl не работает
''' Я пытаюсь получить переменную в html-документ с помощью python и django '''
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)
Можете ли вы попробовать этот фрагмент в более сжатом виде:
from django.shortcuts import render
def nombrarVariables(request):
nombre = 'juan'
context = {'nombre': nombre}
return render(request, 'SeleccionGrafica2.html', context)
В вашем шаблоне вы можете получить доступ к вашей переменной с помощью {{nombre}}
и в функции рендеринга изменить путь к вашим шаблонам относительно вашей файловой структуры