获取您的 Django 服务器
template.html
views.py
 
<!DOCTYPE html>
<html>
<body>

{% regroup cars by brand as newlist %}

{{ newlist }}

{% for x in newlist %}
  <h1>{{ x.grouper }}</h1>
  {% for y in x.list %}
    <p>{{ y.model }}: {{ y.year }}</p>
  {% endfor %}
{% endfor %}

<p>Check out views.py to see what the cars list looks like.</p>

</body>
</html>                  
from django.http import HttpResponse
from django.template import loader

def testing(request):
  template = loader.get_template('template.html')
  context = {
    'cars': [
      {
        'brand': 'Ford',
        'model': 'Mustang',
        'year': '1964',
      },
      {
        'brand': 'Ford',
        'model': 'Bronco',
        'year': '1970',
      },
      {
        'brand': 'Ford',
        'model': 'Sierra',
        'year': '1981',
      },
      {
        'brand': 'Volvo',
        'model': 'XC90',
        'year': '2016',
      },
      {
        'brand': 'Volvo',
        'model': 'P1800',
        'year': '1964',
      }]
    }
  return HttpResponse(template.render(context, request)) 
                  
127.0.0.1:8000/testing