Consistent with the document
This commit is contained in:
parent
e31de53e2f
commit
4ae796df8f
|
@ -10,6 +10,7 @@ A simple example using default setting would be like this:
|
|||
```python
|
||||
from sanic import Sanic
|
||||
from sanic.config import LOGGING
|
||||
from sanic.response import text
|
||||
|
||||
# The default logging handlers are ['accessStream', 'errorStream']
|
||||
# but we change it to use other handlers here for demo purpose
|
||||
|
@ -20,7 +21,7 @@ app = Sanic('test')
|
|||
|
||||
@app.route('/')
|
||||
async def test(request):
|
||||
return response.text('Hello World!')
|
||||
return text('Hello World!')
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(log_config=LOGGING)
|
||||
|
|
|
@ -5,70 +5,70 @@ Use functions in `sanic.response` module to create responses.
|
|||
## Plain Text
|
||||
|
||||
```python
|
||||
from sanic import response
|
||||
from sanic.response import text
|
||||
|
||||
|
||||
@app.route('/text')
|
||||
def handle_request(request):
|
||||
return response.text('Hello world!')
|
||||
return text('Hello world!')
|
||||
```
|
||||
|
||||
## HTML
|
||||
|
||||
```python
|
||||
from sanic import response
|
||||
from sanic.response import html
|
||||
|
||||
|
||||
@app.route('/html')
|
||||
def handle_request(request):
|
||||
return response.html('<p>Hello world!</p>')
|
||||
return html('<p>Hello world!</p>')
|
||||
```
|
||||
|
||||
## JSON
|
||||
|
||||
|
||||
```python
|
||||
from sanic import response
|
||||
from sanic.response import json
|
||||
|
||||
|
||||
@app.route('/json')
|
||||
def handle_request(request):
|
||||
return response.json({'message': 'Hello world!'})
|
||||
return json({'message': 'Hello world!'})
|
||||
```
|
||||
|
||||
## File
|
||||
|
||||
```python
|
||||
from sanic import response
|
||||
from sanic.response import file
|
||||
|
||||
|
||||
@app.route('/file')
|
||||
async def handle_request(request):
|
||||
return await response.file('/srv/www/whatever.png')
|
||||
return await file('/srv/www/whatever.png')
|
||||
```
|
||||
|
||||
## Streaming
|
||||
|
||||
```python
|
||||
from sanic import response
|
||||
from sanic.response import stream
|
||||
|
||||
@app.route("/streaming")
|
||||
async def index(request):
|
||||
async def streaming_fn(response):
|
||||
response.write('foo')
|
||||
response.write('bar')
|
||||
return response.stream(streaming_fn, content_type='text/plain')
|
||||
return stream(streaming_fn, content_type='text/plain')
|
||||
```
|
||||
|
||||
## Redirect
|
||||
|
||||
```python
|
||||
from sanic import response
|
||||
from sanic.response import redirect
|
||||
|
||||
|
||||
@app.route('/redirect')
|
||||
def handle_request(request):
|
||||
return response.redirect('/json')
|
||||
return redirect('/json')
|
||||
```
|
||||
|
||||
## Raw
|
||||
|
@ -76,12 +76,12 @@ def handle_request(request):
|
|||
Response without encoding the body
|
||||
|
||||
```python
|
||||
from sanic import response
|
||||
from sanic.response import raw
|
||||
|
||||
|
||||
@app.route('/raw')
|
||||
def handle_request(request):
|
||||
return response.raw('raw data')
|
||||
return raw('raw data')
|
||||
```
|
||||
|
||||
## Modify headers or status
|
||||
|
@ -89,12 +89,12 @@ def handle_request(request):
|
|||
To modify headers or status code, pass the `headers` or `status` argument to those functions:
|
||||
|
||||
```python
|
||||
from sanic import response
|
||||
from sanic.response import json
|
||||
|
||||
|
||||
@app.route('/json')
|
||||
def handle_request(request):
|
||||
return response.json(
|
||||
return json(
|
||||
{'message': 'Hello world!'},
|
||||
headers={'X-Served-By': 'sanic'},
|
||||
status=200
|
||||
|
|
Loading…
Reference in New Issue
Block a user