diango使用template

1. 在app目录下创建templaes目录

image.png

2. 创建html文件

在templates目录下创建result.html







    
        

        {% for customer in customers %}
            

            {% for name, value in customer.items %}
                
            {% endfor %}

            
        {% endfor %}

        
id 姓名 电话号码 地址
{{ value }}

3. 修改views.py

rom django.shortcuts import render
from django.http import HttpResponse
from common.models import Customer

# Create your views here
def listcustomers(request):
    # 返回一个 QuerySet 对象 ,包含所有的表记录
    # 每条表记录都是是一个dict对象,
    # key 是字段名,value 是 字段值
    qs = Customer.objects.values()
    return render(request, 'result.html', context={"customers": qs})
  1. 在settings.py中加入sqles


    image.png
  2. 重新启动查看


    image.png

你可能感兴趣的:(diango使用template)