๐ Request & Responseยถ
Requestsยถ
Every time a new request comes in, Duck creates a request object from duck.http.request.
This object gives you convenient ways to access all parts of the incoming request.
Request Sessionยถ
Each user has their own session, accessible with request.SESSION or request.session.
Request Queriesยถ
Duck separates query parameters into two types:
URL queries: Parameters from the URL (e.g.
/foo?bar=1)Content queries: Data sent in the request body (e.g. POST form data or JSON)
You can access these with request.{REQUEST_METHOD} (e.g. request.GET, request.POST).
request.GETcontains only URL queries.request.POSTcontains only content/body queries.
If you need both, use:
# views.py
from duck.http.response import HttpResponse
def some_view(request):
print(request.QUERY["CONTENT_QUERY"]) # Shows content/body queries
print(request.QUERY["URL_QUERY"]) # Shows URL queries
return HttpResponse("Hello world")
Request Authenticationยถ
If authentication info is sent in headers, you can access it with request.AUTH.
Request Metadataยถ
All metadata about the request (headers, IP address, etc) is available in request.META.
Request Filesยถ
Uploaded files can be accessed with request.FILES.
To work with a specific file:
some_file = request.FILES["some_file"]
some_file.save() # Save the file if needed.
Responsesยถ
All HTTP responses in Duck are based on duck.http.response.Response.
Duck provides many types of responses, from basic to advanced streaming responses.
You can explore all response types in the duck.http.response module.
What are Streaming Responses?ยถ
Streaming responses let you send large files or data to the client efficientlyโsuch as videos or other big files.
Hereโs how you can create a simple streaming response:
# views.py
from duck.http.response import StreamingHttpResponse
def get_data():
for data in some_data_source:
yield data
def some_view(request):
stream = get_data() # The source can be a function, IO stream, or any iterable
return StreamingHttpResponse(stream)
StreamingRangeHttpResponseยถ
This is an improved version of StreamingHttpResponse that supports partial content (range requests),
allowing users to resume downloads.
To use this, your stream should be a file-like or seekable IO object.
FileResponseยถ
You donโt need to handle partial content yourself.
Use FileResponse to send files: it automatically provides resumable, partial content support.
All FileResponse instances are actually StreamingRangeHttpResponse under the hood.
Response Shortcutsยถ
Duck offers easy-to-use shortcuts for creating common response types, found in duck.shortcuts:
render: Render an HTML template, returning a
TemplateResponse.json: Serialize data as JSON, returning a
JsonResponse.redirect: Create an HTTP redirect with
HttpRedirectResponse.to_response: Create a response from almost any object (strings, components, etc).
Explore the duck.shortcuts module for more!