使用Ajax
- 使用視圖通過上下文向模闆中傳遞資料,需要先加載完成模闆的靜态頁面,再執行模型代碼,生成最張的html,傳回給浏覽器,這個過程将頁面與資料內建到了一起,擴充性差
- 改進方案:通過ajax的方式擷取資料,通過dom操作将資料呈現到界面上
- 推薦使用架構的ajax相關方法,不要使用XMLHttpRequest對象,因為操作麻煩且不容易查錯
- jquery架構中提供了$.ajax、$.get、$.post方法,用于進行異步互動
- 由于csrf的限制,推薦使用$.get
- 示例:實作省市區的選擇
- 最終實作效果如圖:

引入js檔案
- js檔案屬于靜态檔案,建立目錄結構如圖:
修改settings.py關于靜态檔案的設定
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
在models.py中定義模型
class AreaInfo(models.Model):
aid = models.IntegerField(primary_key=True)
atitle = models.CharField(max_length=20)
aPArea = models.ForeignKey('AreaInfo', null=True)
生成遷移
python manage.py makemigrations
python manage.py migrate
通過workbench向表中填充示例資料
- 參見“省市區.sql”
- 注意将表的名稱完成替換
在views.py中編寫視圖
- index用于展示頁面
- getArea1用于傳回省級資料
- getArea2用于根據省、市編号傳回市、區資訊,格式都為字典對象
from django.shortcuts import render
from django.http import JsonResponse
from models import AreaInfo
def index(request):
return render(request, 'ct1/index.html')
def getArea1(request):
list = AreaInfo.objects.filter(aPArea__isnull=True)
list2 = []
for a in list:
list2.append([a.aid, a.atitle])
return JsonResponse({'data': list2})
def getArea2(request, pid):
list = AreaInfo.objects.filter(aPArea_id=pid)
list2 = []
for a in list:
list2.append({'id': a.aid, 'title': a.atitle})
return JsonResponse({'data': list2})
在urls.py中配置urlconf
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index),
url(r'^area1/$', views.getArea1),
url(r'^([0-9]+)/$', views.getArea2),
]
主urls.py中包含此應用的url
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^', include('ct1.urls', namespace='ct1')),
url(r'^admin/', include(admin.site.urls)),
]
定義模闆index.html
- 在項目中的目錄結構如圖:
- 修改settings.py的TEMPLATES項,設定DIRS值
'DIRS': [os.path.join(BASE_DIR, 'templates')],
- 定義模闆檔案:包含三個select标簽,分别存放省市區的資訊
<!DOCTYPE html>
<html>
<head>
<title>省市區清單</title>
</head>
<body>
<select id="pro">
<option value="">請選擇省</option>
</select>
<select id="city">
<option value="">請選擇市</option>
</select>
<select id="dis">
<option value="">請選擇區縣</option>
</select>
</body>
</html>
在模闆中引入jquery檔案
<script type="text/javascript" src="static/ct1/js/jquery-1.12.4.min.js"></script>
編寫js代碼
- 綁定change事件
- 發出異步請求
- 使用dom添加元素
<script type="text/javascript">
$(function(){
$.get('area1/',function(dic) {
pro=$('#pro')
$.each(dic.data,function(index,item){
pro.append('<option value='+item[0]+'>'+item[1]+'</option>');
})
});
$('#pro').change(function(){
$.post($(this).val()+'/',function(dic){
city=$('#city');
city.empty().append('<option value="">請選擇市</option>');
$.each(dic.data,function(index,item){
city.append('<option value='+item.id+'>'+item.title+'</option>');
})
});
});
$('#city').change(function(){
$.post($(this).val()+'/',function(dic){
dis=$('#dis');
dis.empty().append('<option value="">請選擇區縣</option>');
$.each(dic.data,function(index,item){
dis.append('<option value='+item.id+'>'+item.title+'</option>');
})
})
});
});
</script>
1、建立表
定義AreaInfo模型;
複制到虛拟機XP系統,打開navicat,連接配接Ubuntu主機,打開test2資料庫;
打開booktest_areainfo檢視;
點選 查詢-建立查詢
打開areas.sql,複制内容到 查詢編輯器;
打開 編輯-替換;
把資料庫名稱,全部替換為 test2的booktest_areainfo資料庫名稱;
點選 全部替換,完成後,再點選 運作;
完成後,再檢視booktest_areainfo表;
views
建立area模闆;
urls;
測試:
選擇省,
測試:
使用Ajax擷取省市區;
修改settings配置;注冊app,資料庫mysql,模闆templates
模型;
urls跳轉到應用urls;
應用urls;
views,定義index函數
建立檔案夾和模闆;