Add StaticFile and StaticFiles to README

This commit is contained in:
Tom Christie 2018-07-12 13:57:04 +01:00
parent 755de3fa09
commit 9b4db0d62c
1 changed files with 28 additions and 2 deletions

View File

@ -172,6 +172,8 @@ Takes a different set of arguments to instantiate than the other response types:
* `media_type` - A string giving the media type. If unset, the filename or path will be used to infer a media type.
* `filename` - If set, this will be included in the response `Content-Disposition`.
File responses will include appropriate `Content-Length`, `Last-Modified` and `ETag` headers.
```python
from starlette import FileResponse
@ -278,12 +280,12 @@ dispatches to other ASGI applications.
```python
from starlette import Router, Path, PathPrefix
from myproject import Homepage, StaticFiles
from myproject import Homepage, SubMountedApp
app = Router([
Path('/', app=Homepage, methods=['GET']),
PathPrefix('/static', app=StaticFiles, methods=['GET'])
PathPrefix('/mount/', app=SubMountedApp)
])
```
@ -313,6 +315,30 @@ responses for requests which do not match.
---
## Static files
As well as the `FileResponse` class, Starlette also includes ASGI applications
for serving a specific file or directory:
* `StaticFile(path)` - Serve a single file, given by `path`.
* `StaticFiles(directory)` - Serve any files in the given directory.
You can combine these ASGI applications with Starlette's routing to provide
comprehensive static file serving.
```python
from starlette.routing import Router, Path, PathPrefix
from starlette.staticfiles import StaticFile, StaticFiles
app = Router(routes=[
Path('/', app=StaticFile('index.html')),
PathPrefix('/static/', app=StaticFiles(directory='static')),
])
```
---
## Test Client
The test client allows you to make requests against your ASGI application,