#35822: App name hard-coded in collectstatic command forces name of overriding app
name to be 'staticfiles'
-------------------------------------+-------------------------------------
Reporter: fishfin | Owner: (none)
Type: Bug | Status: closed
Component: contrib.staticfiles | Version: 5.1
Severity: Normal | Resolution:
| worksforme
Keywords: collectstatic | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by fishfin):
These are my steps on Windows 11, Python 3.12.7, Django 5.1.2, I use
pipenv: I know some of these steps don't matter, but I am unable to
recreate what you said, so listing everything.
{{{
$ mkdir myvenv & cd myvenv & mkdir .venv & pipenv --python 3.12 & pipenv
shell
$ pipenv install django
$ django-admin.exe startproject myproj & cd myproj
$ django-admin.exe startapp myapp
}}}
Edit myvenv/myproj/myproj/settings.py to:
1) Add `STATIC_ROOT = "static"` to the file
2) Replace item `'django.contrib.staticfiles'` in INSTALLED_APPS with
`'myapp.apps.MyappConfig'` (pointing directly to the Config class instead
of just app name).
Edit myvenv/myproj/myapp/apps.py to:
1) Replace
{{{
from django.apps import AppConfig
class MyappConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'myapp'
}}}
with
{{{
from django.contrib.staticfiles.apps import StaticFilesConfig
class MyappConfig(StaticFilesConfig):
name = 'myapp'
ignore_patterns = ["testpattern"]
}}}
Open shell:
{{{
$ cd myvenv/myproj
$ py manage.py shell
>>> from django.apps import apps
>>> apps.get_app_config('staticfiles').ignore_patterns
LookupError: No installed app with label 'staticfiles'.
>>> apps.get_app_config('myapp').ignore_patterns
['testpattern']
}}}
When you removed `'django.contrib.staticfiles'` from settings.py, I am not
sure how it is still able to find an app with that name. I am getting an
error as I had expected as seen above. Changing MyappConfig.name to
'staticfiles' doesn't help either. Changing parent class of MyappConfig
from StaticFilesConfig to AppConfig does not seem to be right.
Now, since we removed `'django.contrib.staticfiles'`, django doesn't know
about its management/commands directory, so `py manage.py collectstatic`
won't work.
{{{
$ py manage.py collectstatic
Unknown command: 'collectstatic'
Type 'manage.py help' for usage.
}}}
To make it work, I did the following:
{{{
$ mkdir myvenv/myproj/myapp/management/commands
}}}
Create following empty files:
{{{
myvenv/myproj/myapp/management/__init__.py
myvenv/myproj/myapp/management/commands/__init__.py
myvenv/myproj/myapp/management/commands/collectstatic.py
myvenv/myproj/myapp/management/commands/findstatic.py
}}}
Edit `myvenv/myproj/myapp/management/commands/collectstatic.py` and add
following code:
{{{
from django.contrib.staticfiles.management.commands.collectstatic import
Command
}}}
Edit `myvenv/myproj/myapp/management/commands/findstatic.py` and add
following code:
{{{
from django.contrib.staticfiles.management.commands.findstatic import
Command
}}}
Now:
{{{
$ py manage.py collectstatic
File "...\myvenv\.venv\Lib\site-
packages\django\contrib\staticfiles\management\commands\collectstatic.py",
line 103, in set_options
ignore_patterns += apps.get_app_config("staticfiles").ignore_patterns
LookupError: No installed app with label 'staticfiles'.
}}}
...which is the same error I got in django shell. If I am doing something
wrong, please let me know.
--
Ticket URL: <
https://code.djangoproject.com/ticket/35822#comment:3>