# Create your models here. classUser(models.Model): name = models.CharField(max_length=200)
def__str__(self): return self.name
views.py 主要定义了对应路由所响应的函数
from
django.db.models import Count from django.http import HttpResponse from django.shortcuts import render from .models import User
# Create your views here. defindex(request): return HttpResponse('hello world')
defusers(request): field = request.GET.get('field', 'name') user_amount = User.objects.annotate(**{field: Count("name")}) html = "" for u in user_amount: html += "
Amoount of users: {0}
".format(u) return HttpResponse(html)
defloadexampledata(request): u = User(name="Admin") u.save() u = User(name="Staff1") u.save() u = User(name="Staff12") u.save() return HttpResponse("ok")