django-components
django-components copied to clipboard
Documentation: middleware.ComponentDependencyMiddleware not documented
You need to add 'django_components.middleware.ComponentDependencyMiddleware' to your MIDDLEWARE section too, that's the thing that makes smarter choices of where to import CSS and JS.
Originally posted by @EmilStenstrom in https://github.com/EmilStenstrom/django-components/discussions/70#discussioncomment-880635
@rbeard0330 Do you think you could have a stab at this one?
Will do.
I dont want to cut in on what @rbeard0330 is doing but if someone can do a quick loom and show me I dont mind taking a stab at the documentation
I would like to be a user of this feature
@simkimsia I'm trying to remember here, was a couple of years since I played with this. I think you might just add this to your settings.py:
MIDDLEWARE = [
...
'django_components.middleware.ComponentDependencyMiddleware'
]
... and things magically work?
@EmilStenstrom i did and i got this error
ModuleNotFoundError: No module named 'django_components.middleware.ComponentDependencyMiddlewaredjango'; 'django_components.middleware' is not a package
I suspect need to change the __init__.py
?
I am using 0.22 django-components by the way.
Note the last part of the error message: "ComponentDependencyMiddlewaredjango". This means you forgot the comma after your middleware line, so it concatenated that string with the previous one. Try again, with a comma!
Out of interest, would this allow Django to only load Media on pages that the components are used on? This would be ideal so I'm not loading JS for components that aren't used on the page.
@timothyis yes, this is what this middleware does. It’s already in the code, just not documented.
There's two steps needed to enable the middleware: Add the Middleware (as above) and setting the RENDER_DEPENDENCIES key to True in settings.
Fixed by #360