Django模板变量字符串为html

时间:2019-02-01 09:38:04

标签: python django django-templates django-views

我写了一个小视图,其中包含一些嵌入到列表数组之一中的html。当我在模板中使用该列表项时,模板将数据解释为文字字符串而不是html。如何让django不忽略变量内的html标签?

views.py

def home(request, username='Pedro'):
    template_name = 'main/index.html'
    url = "https://www.duolingo.com/users/{}".format(username)
    getUserData(url)
    context = {
        'username': username,
        'wordLists': wordlists,
        'explanations': explanations,
        'words': words,
    }
    # print(context)
    return render(request, template_name, context)


def getUserData(url):
    response = requests.get(url)
    userdata = response.json()

    for language in userdata['language_data']:
        for index in userdata['language_data'][language]['skills']:
            if index.get('levels_finished') > 0:
                wordList = index.get("words")
                wordlists.append(wordList)
                explanations.append(index.get("explanation"))
                for wordItem in wordList:
                    words.append(wordItem)

模板

{% block content %}
  {% for e in explanations %}
    {{ e }}
  {% endfor %}
{% endblock %}

结果 enter image description here

0 个答案:

没有答案