Возврат html-тега путем вызова функции python в шаблоне django
Ребята, на самом деле я ожидаю, что представление django должно возвращать только тег типа
def load_tags():
return httpresponse("<span>span tag</span>")
def home(request):
return render(request, 'index.html',{"func":load_tags()})
html file
<h2>Calling python function {{ func }} </h2>
В браузере он отображается как
- Вызов функции python <HttpResponse status_code=200, "text/html; charset=utf-8"> .
Но я ожидаю как
- Вызов питоновской функции span tag
Ребята, помогите мне достичь этой функциональности
Я попытался решить ваше требование следующим образом...
views.py
def TagsView():
html_h1 = "<h1>Hello django</h1>"
html_italic = "<h4><i>Hello django</i></h4>"
html_hr = "<hr>"
my_code = "<code>print('Hello world')</code>"
return html_h1,html_italic,html_hr,my_code
def DemoView(request):
context = {"func":TagsView()}
return render(request, 'index.html',context)
HTML код
{% block body %}
{% autoescape off %}
{% for i in func %}
{{i}}
{% endfor %}
{% endautoescape %}
{% endblock body %}