Download docxtpl generated file with cherrypy
<p>I am using <code>docxtpl</code> to generate a word document, and wondering how a user can download this file once generated using <code>cherrypy</code>, please see my code below.</p>
<p>The only solution I could come up with is to save it to the <code>www</code> folder and create a link to the location, but I am sure this can be simplified.</p>
<pre><code>import os, os.path
import random
import string
import cherrypy
from docxtpl import DocxTemplate
import sys
from auth import AuthController, require, member_of, name_is
import socket

reload(sys)  
sys.setdefaultencoding('utf8')
cherrypy.config.update({'server.socket_port': 8000})
cherrypy.server.socket_host = '0.0.0.0'
cherrypy.engine.restart()

class Root(object):

    _cp_config = {
        'tools.sessions.on': True,
        'tools.auth.on': True    }

    @cherrypy.expose()
    def default(self, **kwargs):
        print kwargs    
        if kwargs.get('csa_no'):
#             print kwargs.get('csa_no')
            tpl=DocxTemplate('csa_tpl.docx')            
            sd = tpl.new_subdoc()
            p = sd.add_paragraph('This 1st insert')
            sd2 = tpl.new_subdoc()
            p = sd2.add_paragraph('This 2nd insert')
            context1 = {
                    'date': 'jkh',
                    'company_name' : 'Test Footer',
                    'cost' : '10,000',
                    'project_description': kwargs['project_description'],
                    'site': kwargs['site'],
                    'sp': kwargs['sp'],
                    'wo': kwargs['WO'],
                    'contract_manager': kwargs['contract_manager'],
                    'csa_no': kwargs['csa_no'],
                    'variation_reason': kwargs['variation_reason'],
                    'variation_scope_of_works': kwargs['variation_scope_of_works'],
    
                    'Initial_Contract_Value': '$300,000',
                    'variation_total': '$20,000',
                    'Ammended_Contract_Value': '$320,000',
                    'project_manager': kwargs['project_manager'],
                    'construction_manager': 'Dane Wilson',
                    'date': '30/04/2016',    
                }
            tpl.render(context1)
            file_name_with_path = '/var/www/html/csa_documents/' + kwargs['sp'] + '-'+ kwargs['WO'] + '_' + kwargs['site'] + '_-_' + 'CSA' + kwargs['csa_no'] +'.docx'
            file_name = kwargs['sp'] + '-'+ kwargs['WO'] + '_' + kwargs['site'] + '_-_' + 'CSA' + kwargs['csa_no'] +'.docx'
            print file_name
            print file_name_with_path
            tpl.save(file_name_with_path)
            return '''
            
            &lt;!DOCTYPE html&gt;
            &lt;html&gt;
            &lt;head&gt;
            &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;
            &lt;meta charset=&quot;utf-8&quot; http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=9&quot; /&gt;
            &lt;link href=&quot;//ajax.googleapis.com/ajax/libs/jquerymobile/1.4.2/jquery.mobile.min.css&quot; rel=&quot;stylesheet&quot;&gt;
            &lt;script src=&quot;//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js&quot;&gt;&lt;/script&gt;
            &lt;script src=&quot;//ajax.googleapis.com/ajax/libs/jquerymobile/1.4.2/jquery.mobile.min.js&quot;&gt;&lt;/script&gt;
            &lt;title&gt;Broadspectrum&lt;/title&gt;
            &lt;/head&gt;
            &lt;body&gt;
            &lt;div data-role=&quot;header&quot; data-theme=&quot;b&quot;&gt;
            &lt;h1&gt;TCSS Gateway&lt;/h1&gt;
            &lt;/div&gt;
            &lt;h2&gt;Success&lt;/h2&gt;
            &lt;a href=&quot;http://192.168.1.7&quot;&gt;another submission&lt;/a&gt;
            &lt;a href=&quot;http://192.168.1.7/csa_documents/%s&quot;&gt;Download &amp; Review CSA Document&lt;/a&gt;
            &lt;/body&gt;

            ''' % file_name
</code></pre>