尝试将变量mid脚本返回到我的模板

2024-06-20 15:01:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在my home.html上显示动态计数。当for循环运行时,我希望我的模板显示循环的当前计数。这是可能的,如果是的话,这个过程叫什么,这样我就可以了解它了

home.html

<!DOCTYPE html>
<html>
<body>
    {% block content %}
    {% if output >= 1 %}
         <p>Current count is {output}</p>
    {%endif%}
    {% endblock content %}

</body>
</html>

views.py

def page_objects(request):
     output = 0
     for row in c:
          output = output + 1 
          return output
    return render(request, 'home.html')

url.py

from django.urls import path

from . import views

urlpatterns = [
    path('', views.home, name = 'home'),
    path('views', views.page_objects, name = 'page_objects')
]