librelist archives

« back to archive

Overriding default MIME type?

Overriding default MIME type?

From:
Ramon Felciano
Date:
2010-05-17 @ 03:56
Hi --

What is the simplest way to change the MIME type of returned data? I
have a view that is generating CSS and it looks like it is being
returned as HTML by default. Is there a way to simply override the
MIME type, or do I need to create my own fully-populated response
object?

Thanks!

Ramon

Re: [flask] Overriding default MIME type?

From:
Armin Ronacher
Date:
2010-05-17 @ 10:06
Hi,

To change the mimetype you can return a response object directly:

    from flask import Response

    @app.route('/my.css')
    def my_css():
        return Response('/* css here */', mimetype='text/css')

Alternatively you can use Flask to make the response and change the 
mimetype afterwards:

    @app.route('/my.css')
    def my_css():
        rv = app.make_response('/* css here */')
        rv.mimetype = 'text/css'
        return rv


Regards,
Armin

Re: [flask] Overriding default MIME type?

From:
Василь Діжак
Date:
2010-05-17 @ 09:38
I am using werkzeug way
http://werkzeug.pocoo.org/documentation/dev/wrappers.html

2010/5/17 Ramon Felciano <felciano@yahoo.com>

> Hi --
>
> What is the simplest way to change the MIME type of returned data? I
> have a view that is generating CSS and it looks like it is being
> returned as HTML by default. Is there a way to simply override the
> MIME type, or do I need to create my own fully-populated response
> object?
>
> Thanks!
>
> Ramon
>



-- 
"Бути людиною - це відчувати свою відповідальність. Почувати сором перед
убогістю, що, здавалося б, і не залежить від тебе. Пишатися кожною
перемогою, здобутої товаришами. Усвідомлювати, що, кладучи свою цеглу, ти
допомагаєш будувати світ."