[django]手动数据库备份

2018-06-17 18:58:39来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

基本原理是按钮点击后,系统查询出数据表中信息,然后在网页中导出相关表格!

若有自动数据库备份的兄弟,指点一下!

模板代码:

<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

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:【FOL】第一周

下一篇:TImage保存图片到Stream及从Stream中取图片