基本原理是按鈕點擊後,系統查詢出數據表中信息,然後在網頁中導出相關表格!
若有自動數據庫備份的兄弟,指點一下!
模板代碼:
<a href="{% url 'work_backup' %}" class="btn btn-primary btn-sm" type="button">數據備份</a>
url代碼:
keywork_patterns = [ url(r'^backup/$', 'keywork.views.work_backup', name='work_backup'), ] urlpatterns = [ url(r'^$', 'login.views.login_view', name='login_view'), # url(r'^blog/', include('blog.urls')), url(r'^keywork/', include(keywork_patterns)), url(r'^admin/', include(admin.site.urls)), ]
view代碼:
def work_backup(request): response = HttpResponse(content_type='application/vnd.ms-excel') response['Content-Disposition'] = 'attachment; filename=beifen'+time.strftime('%Y%m%d',time.localtime(time.time()))+'.xls' workbook = xlwt.Workbook(encoding='utf-8') #創建工作簿 sheet = workbook.add_sheet("sheet1") #創建工作頁 row0 = [u'用戶標識',u'設備狀態',u'用戶號碼',u'用戶姓名',u'賬戶編碼', u'產品ID',u'支局名稱',u'支局ID',u'區域名稱',u'區域ID', u'網格名稱',u'銷售人員名稱',u'銷售點名稱',u'號碼竣工時間',u'號碼拆機時間', u'用戶類型',u'產品其他標識',u'服務提供ID',u'服務提供名稱',u'CRM竣工時間', u'CRM受理員工',u'CRM受理工號',u'受理點',u'銷售貧名稱',u'統計時間' ] for i in range(0,len(row0)): sheet.write(0,i,row0[i]) data = DevData.objects.values() num = 1 for d in data: sheet.write(num,0,d['serv_id']) sheet.write(num,1,d['serv_state_name']) sheet.write(num,2,d['acc_nbr']) sheet.write(num,3,d['user_name']) sheet.write(num,4,d['acct_code']) sheet.write(num,5,d['product_id']) sheet.write(num,6,d['mkt_chnl_name']) sheet.write(num,7,d['mkt_chnl_id']) sheet.write(num,8,d['mkt_region_name']) sheet.write(num,9,d['mkt_region_id']) sheet.write(num,10,d['mkt_grid_name']) sheet.write(num,11,d['sale_man']) sheet.write(num,12,d['sale_outlets_cd1_name']) sheet.write(num,13,d['completed_time']) sheet.write(num,14,d['remove_data']) sheet.write(num,15,d['user_flag']) sheet.write(num,16,d['pro_flag']) sheet.write(num,17,d['service_offer_id']) sheet.write(num,18,d['service_offer_name']) sheet.write(num,19,d['finish_time']) sheet.write(num,20,d['staff_name']) sheet.write(num,21,d['staff_code']) sheet.write(num,22,d['org_name']) sheet.write(num,23,d['prod_offer_name']) sheet.write(num,24,d['day_id']) num = num + 1 workbook.save(response) return response