diff options
| author | Chris McDonough <chrism@plope.com> | 2012-02-22 20:22:27 -0500 |
|---|---|---|
| committer | Chris McDonough <chrism@plope.com> | 2012-02-22 20:22:27 -0500 |
| commit | c2e82a7505bd0cac9304b31c4e84fbafe521e0e1 (patch) | |
| tree | 8f9b812ef566bfc916eba9f7e0b43d847df43431 /docs/narr | |
| parent | 6b3cca0d548c0c3bcec62902f5b261df4e7c1d1e (diff) | |
| download | pyramid-c2e82a7505bd0cac9304b31c4e84fbafe521e0e1.tar.gz pyramid-c2e82a7505bd0cac9304b31c4e84fbafe521e0e1.tar.bz2 pyramid-c2e82a7505bd0cac9304b31c4e84fbafe521e0e1.zip | |
allow user to pass content type and encoding, change favicon example to use FileResponse
Diffstat (limited to 'docs/narr')
| -rw-r--r-- | docs/narr/assets.rst | 15 |
1 files changed, 9 insertions, 6 deletions
diff --git a/docs/narr/assets.rst b/docs/narr/assets.rst index 2cc870619..bad666066 100644 --- a/docs/narr/assets.rst +++ b/docs/narr/assets.rst @@ -370,19 +370,22 @@ do so, do things "by hand". First define the view callable. :linenos: import os - from pyramid.response import Response + from pyramid.response import FileResponse def favicon_view(request): here = os.path.dirname(__file__) - icon = open(os.path.join(here, 'static', 'favicon.ico'), 'rb') - return Response(content_type='image/x-icon', app_iter=icon) + icon = os.path.join(here, 'static', 'favicon.ico') + return FileResponse(icon, request=request) The above bit of code within ``favicon_view`` computes "here", which is a path relative to the Python file in which the function is defined. It then uses the Python ``open`` function to obtain a file handle to a file within -"here" named ``static``, and returns a response using the open the file -handle as the response's ``app_iter``. It makes sure to set the right -content_type too. +"here" named ``static``, and returns a :class:`pyramid.response.Fileresponse` +using the file path as the response's ``path`` argument and the request as +the response's ``request`` argument. :class:`pyramid.response.FileResponse` +will serve the file as quickly as possible when it's used this way. It makes +sure to set the right content length and content_type too based on the file +extension of the file you pass. You might register such a view via configuration as a view callable that should be called as the result of a traversal: |
