url配置
urlpatterns=[
url(r'^$',views.index),
url(r'^pro/$',views.pro),
url(r'^city(\d+)/$',views.city)
]
模型
from django.db import models
class AreaInfo(models.Model):
title=models.CharField(max_length=20)
parea=models.ForeignKey('self',null=True,blank=True)
定义视图
from django.shortcuts import render
from django.http import JsonResponse
from models import *
def index(request):
return render(request,'booktest/index.html')
def pro(request):
prolist=AreaInfo.objects.filter(parea__isnull=True)
list=[]
for item in prolist:
list.append([item.id,item.title])
return JsonResponse({
'data':list})