Note that there are some explanatory texts on larger screens.

plurals
  1. POPylons: response renaming? Is there a better way?
    primarykey
    data
    text
    <p>I've got a Pylons controller with an action called <strong>serialize</strong> returning <code>content_type=text/csv</code>. I'd like the response of the action to be named based on the input patameter, i.e. for the following route, produced csv file should be named <code>{id}.csv</code> : <code>/app/PROD/serialize</code> => <code>PROD.csv</code> (so a user can open the file in Excel with a proper name directly via a webbrowser)</p> <pre><code>map.connect('/app/{id}/serialize',controller = 'csvproducer',action='serialize') </code></pre> <p>I've tried to set different HTTP headers and properties of the webob's response object with no luck. However, I figured out a workaround by simply adding a new action to the controller and dynamically redirecting the original action to that new action, i.e.:</p> <pre><code>map.connect('/app/{id}/serialize',controller = 'csvproducer',action='serialize') map.connect('/app/csv/{foo}',controller = 'csvproducer', action='tocsv') </code></pre> <p>The controller's snippet:</p> <pre><code>def serialize(self,id): try: session['key'] = self.service.serialize(id) #produces csv content session.save() redirect_to(str("/app/csv/%s.csv" % id)) except Exception,e: log.error(e) abort(503) def tocsv(self): try: csv = session.pop("rfa.enviornment.serialize") except Exception,e: log.error(e) abort(503) if csv: response.content_type='text/csv' response.status_int=200 response.write(csv) else: abort(404) </code></pre> <p>The above setup works perfectly fine, however, is there a better/slicker/neater way of doing it? Ideally I wouldn't like to redirect the request; instead I'd like to either rename <code>location</code> or set <code>content-disposition: attachment; filename='XXX.csv'</code> [ unsuccessfully tried both :( ]</p> <p>Am I missing something obvious here?</p> <p>Cheers</p> <p><strong>UPDATE:</strong> Thanks to <strong><em>ebo</em></strong> I've managed to do fix <code>content-disposition</code>. Should better read W3C specs next time ;)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload