#34712 closed Cleanup/optimization (fixed)

Prevent misconfiguration of `STORAGES` setting

Reported by: Bruno Alla Owned by: Bruno Alla
Component: contrib.staticfiles Version: 4.2
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

The new STORAGES setting added in #26029 is great and super flexible. While trying to migrate Cookiecutter Django to use it [1], I noticed that it's quite easy to break the configuration of one of the storages: if I want to override only one of the 2 default storages (e.g. staticfiles), then I might be tempted to set:

STORAGES = {
    "staticfiles": {"BACKEND": "MyCustomStorage"},
}

Which would raise a InvalidStorageError when trying to upload a FileField somewhere. AFAIK, nothing warns the user of this misconfiguration until this point.

I think that one might assume that the dictionary defined by the user would be "merged" with the default value. I'm assuming that implementing behaviour could be considered too much of breaking change to the behaviour etablished in 4.2, but I think the documentation could outline better the existing behaviour when only one storage is defined by the user.

Another thing that could be maybe be added is a deployment check, to make sure that both default and staticfiles storages are present in the setting.

[1] https://github.com/cookiecutter/cookiecutter-django/pull/4457

Change History (9)

comment:1 by Bruno Alla, 17 months ago

Summary: Improve misconfiguration of `STORAGES` settingPrevent misconfiguration of `STORAGES` setting

comment:2 by Mariusz Felisiak, 17 months ago

Component: Documentationcontrib.staticfiles
Triage Stage: UnreviewedAccepted

Documentation improvements are welcome.

Another thing that could be maybe be added is a deployment check, to make sure that both default and staticfiles storages are present in the setting.

I agree that we could add a check for a staticfiles storage, e.g.

  • staticfiles.E005: You must define a 'staticfiles' storage in your :setting:STORAGES setting. in the django.contrib.staticfiles.checks.

However, as far as I'm aware, you don't have to use the default storage so a new system check might be an issue for some projects.

comment:3 by Bruno Alla, 17 months ago

Owner: changed from nobody to Bruno Alla
Status: newassigned

comment:4 by Bruno Alla, 17 months ago

Has patch: set

comment:5 by Mariusz Felisiak, 17 months ago

Patch needs improvement: set

comment:6 by Mariusz Felisiak <felisiak.mariusz@…>, 17 months ago

In 86561844:

Refs #34712 -- Doc'd that defining STORAGES overrides the default configuration.

comment:7 by Mariusz Felisiak <felisiak.mariusz@…>, 17 months ago

In 12ebd9a1:

[4.2.x] Refs #34712 -- Doc'd that defining STORAGES overrides the default configuration.

Backport of 86561844ce66cda3e6a8c22d4ace4c2d1bc1f2e7 from main

comment:8 by Mariusz Felisiak <felisiak.mariusz@…>, 16 months ago

In 4c4536f7:

Refs #34712 -- Added system check for staticfiles storage in STORAGES setting.

Co-authored-by: Mariusz Felisiak <felisiak.mariusz@…>
Co-authored-by: Natalia Bidart <124304+nessita@…>

comment:9 by Mariusz Felisiak, 16 months ago

Resolution: fixed
Status: assignedclosed
Note: See TracTickets for help on using tickets.
Back to Top