I get it to work like this :

def export_csv():
    rows = db((db.tab1.id == tab1_id)).select()
    return dict(rows=rows)

def export_default_analysis_request():
    tab1_subset = (db.tab1.bool1 == True)
    form = SQLFORM.factory(
            Field('tab1_id', requires=IS_IN_DB(db(tab1_subset),'tab1.id
','%(f1)s')),
                submit_button=T('Submit'))
    if form.accepts(request.vars, session):
        response.flash = T('form accepted')
      * redirect(URL(a=request.application, c='C', f='export_csv.csv',
vars=dict(tab1_id=form.vars.tab1_id)))*
    elif form.errors:
        response.flash = T('form has errors')
    else:
        response.flash = T('please fill out the form')
    return dict(form=form)

And I put this in /views/C/export_csv.csv as explain in the book :

{{
import cStringIO
stream=cStringIO.StringIO()
rows.export_to_csv_file(stream)
response.headers['Content-Type']='application/vnd.ms-excel'
response.write(stream.getvalue(), escape=False)
}}

And it works fine, the only thing is that I get a blank page... I could
probably set a ajax call instead of redirect later.

Richard


On Wed, Mar 14, 2012 at 8:18 PM, Richard Vézina <ml.richard.vez...@gmail.com
> wrote:

> Ho yeah, I forget about that option...
>
> Thanks Alan
>
> Richard
>
>
> On Wed, Mar 14, 2012 at 6:53 PM, Alan Etkin <spame...@gmail.com> wrote:
>
>> I have not experience with the book csv output example, but it looks
>> different than your implementation:
>>
>> -It has a special view "..../name.csv" to render the function output
>> -The Content-Type parameter is "application-vnd.ms-excel"
>>
>> I think you could change the response view parameter on form
>> validation to .../name.csv, have the function to return a dict object
>> and use the book example view to return the csv file response.
>>
>> The example I am refering to is in section 10.1.6
>>
>> On 14 mar, 18:19, Richard <ml.richard.vez...@gmail.com> wrote:
>> > Hello,
>> >
>> > I strungle with export of csv... I have this function that works great :
>> >
>> > def export_csv(tab1_id):
>> >     import gluon.contenttype
>> >     response.headers['Content-Type'] = \
>> >         gluon.contenttype.contenttype('.csv')
>> >     import cStringIO
>> >     stream=cStringIO.StringIO()
>> >     db((db.tab1.id == tab1_id)).select().export_to_csv_file(stream)
>> >     response.headers['Content-disposition'] = 'attachment;
>> filename=%s.csv'
>> > % 'test12345'
>> >     response.write(stream.getvalue())
>> >
>> > def export_default_analysis_request():
>> >     tab1_subset = (db.tab1.bool1 == True)
>> >     form = SQLFORM.factory(
>> >             Field('tab1_id',
>> > requires=IS_IN_DB(db(tab1_subset),'tab1.id','%(f1)s')),
>> >                 submit_button=T('Submit'))
>> >     if form.accepts(request.vars, session):
>> >         response.flash = T('form accepted')
>> >         export_csv(tab1_id = form.vars.tab1_id)
>> >     elif form.errors:
>> >         response.flash = T('form has errors')
>> >     else:
>> >         response.flash = T('please fill out the form')
>> >     return dict(form=form)
>> >
>> > The problem is that I am getting the html page below the value I want to
>> > export...
>> >
>> > Do I need to redirect on a blank page? It likes if response contain the
>> > actual page that get out at the same time as the data...
>> >
>> > I just copy stuff from here and there, but I think I will have to better
>> > understand what is going on...
>> >
>> > Thanks
>> >
>> > Richard
>>
>
>

Reply via email to