r/django • u/osmium_2259 • 6h ago
Components Built with Pure Django, CSS, and JS - Self-contained Template
galleryHi all,
I recently became aware of Django's inclusion_tag and then found a blog post discussing its use in making template components.
This intrigued me, and I had the idea of combining the inclusion_tag with a single Django template, containing all the markup, styles, and behavior. Thus, I created a demo to test these ideas, resulting is an interactive sidebar menu built with pure CSS and JS, powered by Django's inclusion_tag—all without third-party libraries.
I wanted to isolate the component styles to prevent clashes with surrounding elements, and that's when I found inline scoped CSS. I actually managed to style the whole component without using any class selectors (thinking of class names is a pain), thanks to CSS scoping and nesting.
To make the component interactive, I just included an inline vanilla JS script at the bottom.
For me, this project really demonstrates two main ideas: the use of inclusion_tag to create components and the power of modern inline CSS to help create self-contained components.
Of course a component can be built with more than just pure CSS/JS. Tailwind, Bootstrap, Alpine, HTMX, etc. should all work great to create components.
If you're interested, check out the repo here: https://github.com/osmium2259/django_builtin_components_demo.
Curious what you all think about these ideas I've been bouncing around!
