How to send a raw http header in python just like header() in PHP ?
Asked
Active
Viewed 1,462 times
2 Answers
1
Pass a list of two-tuples containing the header name and header value to the start_response()
function.

Ignacio Vazquez-Abrams
- 776,304
- 153
- 1,341
- 1,358
1
In Django, you'd be like:
def someview(request):
# ... etc ...
out = HttpResponse(outputstring,
mimetype="text/html",
status_code="302",
)
out['Content-Disposition'] = "attachment; filename=download.html"
# fill in all your favorite HTTP headers here
return out
... for cache-control and friends, you need to import a bunch of decorators and wrap your view functions accordingly (I forget which) -- this is because django has a caching system with which many sub-rosa bits of the framework are integrated.
I find the cache stuff confusing, but also nice. non-cache HTTP headers are E-Z.

fish2000
- 4,289
- 2
- 37
- 76
-
that probably works -- but I know that the django people think that's a bad idea as they redefine sys.stdout and friends. using `print` for headers will probably break at some point. – fish2000 Oct 19 '10 at 21:37