首页 > 解决方案 > django 视图循环在 html 模板中不起作用

问题描述

我在 views.py 文件中创建了循环,然后通过 dictionary 。我已经在 django 项目的 html 文件中连接了该循环。它不工作。

视图.py

def count(request):
    data= request.GET['fulltextarea']
    text=data.split()
    counted_text=len(text)

    worddictionary={}

    for word in text:
        if word in worddictionary:
            worddictionary[word] += 1
        else:
            worddictionary[word] = 1

    return render(request,'count.html',{'fulltextarea':data, 'len':counted_text,'worddictionary': worddictionary})

html中出现错误

<body> <h1>Counted</h1> 
<h2> Your text</h2> 
{{fulltextarea}} <br> 
<h2>Words count</h2> 
Total word = {{len}} <br> 
<h2>Number of each word</h2> 
{% for word, value in worddictionary %} 
{{ word }} = {{ value }} 
{% endfor %} 
</body>

标签: djangodjango-templatesdjango-2.2

解决方案


推荐阅读