views: from django.shortcuts import render,redirect from django.shortcuts import HttpResponse # Create your views here. from django.views import View class Home(View): def dispatch(self, request, *args, **kwargs): print('before') result = super(Home,
当列表为空或者非空时执行不同操作: {% for item in list %} ... {% empty %} ... {% endfor %} 使用forloop.counter访问循环的次数,下面这段代码依次输出循环的次数,从1开始计数: {% for item in list %} ... {{ forloop.counter }} ... {% endfor %} 从0开始计数: {% for item in list %} ... {{ forloop.counter0 }} ...