from django.shortcuts import render
from django.http import HttpResponse,JsonResponse
from project1.settings import MEDIA_ROOT #导入上传文件保存路径 或 from django.conf import settingsfrom app1.models import PicTest,AreaInfo #导入图片模型类# /indexdefindex(request):return render(request,'app1/index.html')# /areas 省市下拉联动展示页defareas(request):return render(request,'app1/areas.html')# /prov 处理ajax请求,返回所有省defprov(request):
areas=AreaInfo.objects.filter(aParent__isnull=True)
prov=[]for p in areas:
prov.append((p.id,p.atitle))return JsonResponse({'data':prov})#/citydefcity(request, pid):'''获取pid的下级地区的信息'''# 1.获取pid对应地区的下级地区#获取方法1# area = AreaInfo.objects.get(id=pid)# areas = area.areainfo_set.all()#获取方法2
areas = AreaInfo.objects.filter(aParent__id=pid)# 2.变量areas并拼接出json数据:atitle id
areas_list =[]for area in areas:
areas_list.append((area.id, area.atitle))# 3.返回数据return JsonResponse({'data': areas_list})