EmilStenstrom / django-components

Create simple reusable template components in Django.
MIT License
973 stars 62 forks source link

Documentation: middleware.ComponentDependencyMiddleware not documented #71

Closed EmilStenstrom closed 4 months ago

EmilStenstrom commented 2 years ago

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

EmilStenstrom commented 2 years ago

@rbeard0330 Do you think you could have a stab at this one?

rbeard0330 commented 2 years ago

Will do.

simkimsia commented 1 year ago

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

EmilStenstrom commented 1 year ago

@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?

simkimsia commented 1 year ago

@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.

EmilStenstrom commented 1 year ago

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!

timothyis commented 1 year ago

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.

EmilStenstrom commented 1 year ago

@timothyis yes, this is what this middleware does. It’s already in the code, just not documented.

EmilStenstrom commented 9 months ago

There's two steps needed to enable the middleware: Add the Middleware (as above) and setting the RENDER_DEPENDENCIES key to True in settings.

EmilStenstrom commented 4 months ago

Fixed by #360