Content Disposition
A library that allows easy management of 'Content-Disposition' details of HTTP responses.
So far it provides:
- RFC 5987 (file response, https://datatracker.ietf.org/doc/html/rfc5987)
Installation
- Install using pip:
pip install content-disposition
Usage
Use the rfc5987_content_disposition function to set an HTTP response's 'Content-Type' field in any endpoint, e.g.:
# within views.py
from django.http import FileResponse
from rest_framework import viewsets
from rest_framework.decorators import action
from content_disposition import rfc5987_content_disposition
class MyViewSet(viewsets.ModelViewSet):
...
@action(
detail=True,
methods="get",
url_path=r"download",
)
def download_route(self, request, pk=None):
"""
Assuming that self.get_object() returns a model defining
'name = models.CharField(...)', 'file = models.FileField(...)' and 'mime = models.CharField(...)'
whereas 'mime' represents the correct mime_type related to 'file'
"""
instance = self.get_object()
response = FileResponse(
instance.file,
content_type=instance.mime,
)
response["Content-Disposition"] = rfc5987_content_disposition(instance.name)
return response
...
Run tests in PyCharm
The package comes with a Django test app to verify functionalities in a realistic environment. To locally run the provided test cases, e.g. in your IntelliJ IDEA, you can:
- Configure the interpreter
- Go to
File->Settings->Project: content-disposition->Python interpreter AddaVirtualenv Environmentand configure the correctLocationand checkInherit global site-packages:
- Go to
- Configure the Django framework
- Go to
File->Settings->Languages & Frameworks->Django - Check
Enable Django Supportand configure the correct project root, settings and manage script location:
- Go to
- Install the requirements by running:
pip install -r requirements.txtin a terminal within your venv - Right click on the
testsfolder and selectRun 'Test:' - The "Run" window should list all (successfully) executed test cases:
Metadata
Release files for content-disposition 1.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| content-disposition-1.2.0.tar.gz | 3.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| content_disposition-1.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 7.9 kB
Release files / content-disposition-1.2.0.tar.gz
| Download URL | content-disposition-1.2.0.tar.gz |
|---|---|
| Size | 3.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5f7d955f4d9c0c85563de09ca21f9ebaac960f107d9a0f905728ac5961ec42c9
|
|
BLAKE2b-256 checksum How to use checksums |
b5de38bfa1e87141810c024ee2ddd237cc145ee75641a9cd8bb69b438ca565f2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.20
|
Release files / content_disposition-1.2.0-py3-none-any.whl
| Download URL | content_disposition-1.2.0-py3-none-any.whl |
|---|---|
| Size | 4.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bfc04dd7ad3e6b32a0b61e9f8e290e7711348c6bcb799b45e9b994752df14085
|
|
BLAKE2b-256 checksum How to use checksums |
b44454de305c65984c6ec39799212a117d2131cb375337eeab9c5d0326434576
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.20
|