Pyt*_*ast 5 python pdf django jquery
我通过AJAX获得2个日期,开始和结束日期.我处理数据b/w那两个日期,生成一个报告,然后返回一个HttpResponse.PDF报告现在保存在我的主项目目录中.现在我在AJAX中得到回复.那么,现在我应该如何处理成功函数中的响应,从服务器发回并打开PDF文件.
谢谢.
jQuery的
$(function() {
$("#report_submit").click(function(){
$.ajax({
type : "POST",
url: "/reports/",
data : { 'start_date' : $("#startDate").val() , 'end_date' : $("#endDate").val() },
success : function(result){
},
error : function(result){
}
});
});
});
Run Code Online (Sandbox Code Playgroud)
Django查看代码
def generate_report(request):
ctx = {}
if request.is_ajax():
if request.POST.has_key('start_date'):
start_date = datetime.strptime(request.POST[ 'start_date'] , '%m/%d/%Y')
end_date = datetime.strptime(request.POST[ 'end_date'] , '%m/%d/%Y')
......
# PDF GENERATED in MAIN PROJECT DIRECTORY
with open(os.path.join(os.path.dirname(__file__),'../../../../gui','Report.pdf')) as pdf:
response = HttpResponse(pdf.read(), content_type='application/pdf')
response['Content-Disposition'] = 'inline;filename=Report.pdf'
return response # so, now when I send a response back, how should I process it in AJAX success function?
pdf.closed
return render(request, 'generate_report/reports.html', ctx)
Run Code Online (Sandbox Code Playgroud)
这个问题已经在下面的问题中讨论过...您可能需要使用 jquery 插件来下载文件,请不要忘记在响应中设置 cookie。
您可能需要添加一个 javascript 文件来下载文件,并使用以下代码生成对服务器的请求。
$.fileDownload(urlll,{
successCallback: function (url)
{
//success code here
},
failCallback: function (html, url)
{
//error code here
}
});
Run Code Online (Sandbox Code Playgroud)
在服务器端,在响应中添加标头等时,在响应对象中执行以下操作。IE
aResponse.addCookie(cookie);
Run Code Online (Sandbox Code Playgroud)
我希望你能解决这个问题,也能帮助其他人。“悬空指针”