为了实现此功能,需要用到的知识点:

(1)django  admin  自定义字段;

    参考:http://www.cnblogs.com/wumingxiaoyao/p/6928297.html

(2)使用 docxtpl 模块 对word文档内容 进行替换;

    参考:http://www.cnblogs.com/wumingxiaoyao/p/8315814.html

(3)使用django 的  FileResponse 模块 实现文档下载;

参考:http://blog.csdn.net/lmw1239225096/article/details/79047375

主要代码:

url.py:

from django.conf.urls import url
from hys_operation import views urlpatterns = [
# url(r'^sub_users/(?P<obj_id>\d+)', views.get_sub_users),
# url(r'^sub_servers/(?P<obj_id>\d+)', views.get_sub_servers),
url(r'^download/(?P<paper_num>\w+)', views.download, name='download'),
]

views.py:

from django.http import FileResponse

def download(request, paper_num):
"""
下载数据备案单
:param request:
:param paper_num: 备案单号
:return: 数据流
"""
# file_path = '/webserver/hys_cmdb/static/download/'
file_path = 'E:\\myweb\\hys_cmdb\\static\\download\\'
file = open("{}{}.docx".format(file_path, paper_num), "rb")
response = FileResponse(file)
response['Content-Type'] = 'application/octet-stream'
response['Content-Disposition'] = 'attachment;filename="{}.docx"'.format(paper_num)
return response

admin.py:

class DataPaperStoreAdmin(admin.ModelAdmin):

    ......

    def down_paper(self, obj):
"""自定义一个a标签,跳转到实现下载功能的url"""
dest = '{}export/'.format(obj.pk)
title = '下载'
return '<a href="{}">{}</a>'.format(dest, title)
down_paper.short_description = '下载数据单'
down_paper.allow_tags = True def get_urls(self):
"""添加一个url,指向实现下载功能的函数make_docx"""
from django.conf.urls import url
urls = [
url('^(?P<pk>\d+)export/?$',
self.admin_site.admin_view(self.make_docx),
name='export_data'),
]
return urls + super(DataPaperStoreAdmin, self).get_urls() def make_docx(self, request, *args, **kwargs):
from docxtpl import DocxTemplate
import re
file_path = 'E:\\myweb\\hys_cmdb\\static\\download\\'
# file_path = '/webserver/hys_cmdb/static/download/'
obj = get_object_or_404(DataPaperStore, pk=kwargs['pk'])
list_nums = re.findall("\d+", obj.paper_num) # 获取字符串中的所有数字
nums = ''.join(list_nums)
doc = DocxTemplate("{}export.docx".format(file_path))
context = {'paper_num': obj.paper_num,
'project_name': obj.project_name,
'to_mail': obj.to_mail,
'data_selected': obj.data_selected,
'start_date': obj.start_date,
'end_date': obj.end_date,
}
doc.render(context)
doc.save("{}{}.docx".format(file_path, nums))
new_path = reverse('download', args=(nums,))
return HttpResponseRedirect(new_path) list_display = ('project_name', 'to_mail', 'proposer', 'frequency', 'commit_date',
'start_date', 'end_date', 'colored_paper_num', 'is_sure', 'expired', 'down_paper',)

  

效果:

django admin 中实现word文档下载的更多相关文章

  1. C# 中使用Word文档对图像进行操作

    C# 中使用Word文档对图像进行操作 Download Files: ImageOperationsInWord.zip 简介 在这篇文章中我们可以学到在C#程序中使用一个Word文档对图像的各种操 ...

  2. 如何在程序中给word文档加上标和下标

    如何在程序中给word文档加上标和下标 上标或下标是一个小于普通行格式的数字,图形,标志或者指示通常它的设置与行相比偏上或偏下.下标通常显示于或者低于基准线,而上标则高于.上标和下标通常被用于表达公式 ...

  3. 在Delphi中处理word文档与数据库的互联 1

    在Delphi中处理word文档与数据库的互联 ---- 目前,Delphi被越来越多的人选中作为MIS系统开发中的前台工具.在以Delphi为前台,一些大型数据库为后台的MIS系统中,图形的处理不可 ...

  4. 在Delphi中处理word文档与数据库的互联

    在Delphi中处理word文档与数据库的互联 ---- 目前,Delphi被越来越多的人选中作为MIS系统开发中的前台工具.在以Delphi为前台,一些大型数据库为后台的MIS系统中,图形的处理不可 ...

  5. c#中操作word文档-四、对象模型

    转自:http://blog.csdn.net/ruby97/article/details/7406806 Word对象模型  (.Net Perspective) 本文主要针对在Visual St ...

  6. SpringBoot+FreeMarker开发word文档下载,预览

    背景: 开发一个根据模版,自动填充用户数据并下载word文档的功能 使用freemarker进行定义模版,然后把数据进行填充. maven依赖: <parent> <groupId& ...

  7. ASP.NET MVC 拓展ViewResult实现word文档下载

      最近项目中有同事用到word文档导出功能,遇到了一些导出失败问题,帮其看了下解决问题的同事,看了下之前的代码发现几个问题: 代码编写不规范,word导出功能未收口 重复代码导出都是 实现逻辑比较复 ...

  8. c#中操作word文档-一、模板方式写入

    转载自:http://blog.csdn.net/fujie724/article/details/5443322 适合模板写入 今天正好有人问我,怎么生成一个报表式的Word文档. 就是文字的样式和 ...

  9. 在C#中创建word文档

    在下面文档中  首先引用word组件:Microsoft.Office.Interop.Word 在头文件中写上 using Word = Microsoft.Office.Interop.Word; ...

随机推荐

  1. spi总结

    SPI的四种模式 SPI的相位(CPHA)和极性(CPOL)分别可以为0或1,对应的4种组合构成了SPI的4种模式(mode) Mode 0:CPOL = 0,CPHA = 0 Mode 1:CPOL ...

  2. SqlServer使用CONVERT 对时间进行格式化

    前言 在最近使用SqlServer的时候遇到时间格式的转换,特此记录下. 本文参考:https://www.cnblogs.com/xiaoleiel/p/8301027.html,如有侵权,请联系删 ...

  3. 生成代码,从 T1 到 T16 —— 自动生成多个类型的泛型

    当你想写一个泛型 的类型的时候,是否想过两个泛型参数.三个泛型参数.四个泛型参数或更多泛型参数的版本如何编写呢?是一个个编写?类小还好,类大了就杯具! 事实上,在 Visual Studio 中生成代 ...

  4. LeetCode 529. Minesweeper

    原题链接在这里:https://leetcode.com/problems/minesweeper/description/ 题目: Let's play the minesweeper game ( ...

  5. 同一台电脑上装两个或两个以上的tomcat服务器

    1.下载免安装版tomcat,解压成tomcat1.tomcat2: 2.修改tomcat2中conf下server.xml文件如下: <Server port="8005" ...

  6. kong 安装

    1. yum 参考信息 https://bintray.com/kong/kong-community-edition-rpm $ sudo yum install epel-release $ su ...

  7. linux 磁盘挂载操作

    1. fdisk  -l      查看磁盘   2. fisk  /dev/vdb   进行分区        依次输入  n  p  1   两次回车  wq   3. fdisk -l  查看分 ...

  8. SysTick_Config

    SystemCoreClockUpdate();SysTick_Config(SystemCoreClock/2000);   //500us

  9. STM32学习笔记之__attribute__ ((at())绝对定位分析

    STM32也会遇到这样的绝对定位的问题如下: uint8_t   UART_RX_BUF[1024]   __attribute__ ((at(0X20001000)));   //就是将串口接收的数 ...

  10. POJ 3684 Physics Experiment(弹性碰撞)

    Physics Experiment Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2936   Accepted: 104 ...