Create simple reusable template components in Django. https://django-components.github.io/django-components
Find a file
Emil Stenström d407a8cd13 Update marks.
2025-01-22 23:51:12 +01:00
.github refactor: change repo name and URL after org migration (#924) 2025-01-22 16:02:46 +01:00
benchmarks refactor: Patch Template.compile_nodelist with custom template parser (#908) 2025-01-15 22:34:32 +01:00
docs docs: update auto-gen'd docs to show signals reference page (#926) 2025-01-22 16:32:00 +01:00
logo Update marks. 2025-01-22 23:51:12 +01:00
sampleproject refactor: rename template_name to template_file (#878) 2025-01-01 17:06:14 +01:00
scripts docs: Move docs-folder to root (#816) 2024-12-03 12:32:21 +01:00
src refactor: change repo name and URL after org migration (#924) 2025-01-22 16:02:46 +01:00
tests refactor: change repo name and URL after org migration (#924) 2025-01-22 16:02:46 +01:00
.gitignore fix: TemplateDoesNotExist when using {% extends %} on main template and two components with same parent template (#862) 2024-12-23 12:58:03 +01:00
.pre-commit-config.yaml [pre-commit.ci] pre-commit autoupdate (#700) 2024-10-08 08:46:43 +02:00
CHANGELOG.md refactor: change repo name and URL after org migration (#924) 2025-01-22 16:02:46 +01:00
CODE_OF_CONDUCT.md Update CODE_OF_CONDUCT.md 2024-01-17 09:40:07 +01:00
LICENSE Create LICENSE 2019-11-28 21:38:10 +01:00
MANIFEST.in feat: add JS dependency manager (#666) 2024-09-22 16:42:41 +02:00
mkdocs.yml refactor: change repo name and URL after org migration (#924) 2025-01-22 16:02:46 +01:00
pyproject.toml chore: bump v0.125 (#925) 2025-01-22 16:09:05 +01:00
README.md refactor: change repo name and URL after org migration (#924) 2025-01-22 16:02:46 +01:00
requirements-ci.in feat: add JS dependency manager (#666) 2024-09-22 16:42:41 +02:00
requirements-ci.txt build(deps): bump tox from 4.23.2 to 4.24.1 2025-01-21 20:38:09 +00:00
requirements-dev.in refactor: replace selectolax with beautifulsoup (#823) 2024-12-08 08:42:48 +01:00
requirements-dev.txt build(deps): bump tox from 4.23.2 to 4.24.1 2025-01-21 20:38:09 +00:00
requirements-docs.txt build(deps): bump tzdata from 2024.2 to 2025.1 2025-01-21 20:22:19 +00:00
tox.ini Revert "build(deps): bump playwright from 1.48.0 to 1.49.0" (#820) 2024-12-04 21:19:52 +01:00

django-components

PyPI - Version PyPI - Python Version PyPI - License PyPI - Downloads GitHub Actions Workflow Status

Read the full documentation

⚠️ Attention ⚠️ - We migrated from EmilStenstrom/django-components to django-components/django-components.

Repo name and documentation URL changed. Package name remains the same.

Report any broken links links in #922.

Django-components is a package that introduces component-based architecture to Django's server-side rendering. It aims to combine Django's templating system with the modularity seen in modern frontend frameworks.

A component in django-components can be as simple as a Django template and Python code to declare the component:

<div class="calendar">
  Today's date is <span>{{ date }}</span>
</div>
from django_components import Component

class Calendar(Component):
    template_file = "calendar.html"

Or a combination of Django template, Python, CSS, and Javascript:

<div class="calendar">
  Today's date is <span>{{ date }}</span>
</div>
.calendar {
  width: 200px;
  background: pink;
}
document.querySelector(".calendar").onclick = function () {
  alert("Clicked calendar!");
};
from django_components import Component

class Calendar(Component):
    template_file = "calendar.html"
    js_file = "calendar.js"
    css_file = "calendar.css"

Alternatively, you can "inline" HTML, JS, and CSS right into the component class:

from django_components import Component

class Calendar(Component):
    template = """
      <div class="calendar">
        Today's date is <span>{{ date }}</span>
      </div>
    """

    css = """
      .calendar {
        width: 200px;
        background: pink;
      }
    """

    js = """
      document.querySelector(".calendar").onclick = function () {
        alert("Clicked calendar!");
      };
    """

Features

  1. 🧩 Reusability: Allows creation of self-contained, reusable UI elements.
  2. 📦 Encapsulation: Each component can include its own HTML, CSS, and JavaScript.
  3. 🚀 Server-side rendering: Components render on the server, improving initial load times and SEO.
  4. 🐍 Django integration: Works within the Django ecosystem, using familiar concepts like template tags.
  5. Asynchronous loading: Components can render independently opening up for integration with JS frameworks like HTMX or AlpineJS.

Potential benefits:

  • 🔄 Reduced code duplication
  • 🛠️ Improved maintainability through modular design
  • 🧠 Easier management of complex UIs
  • 🤝 Enhanced collaboration between frontend and backend developers

Django-components can be particularly useful for larger Django projects that require a more structured approach to UI development, without necessitating a shift to a separate frontend framework.

Quickstart

django-components lets you create reusable blocks of code needed to generate the front end code you need for a modern app.

Define a component in components/calendar/calendar.py like this:

@register("calendar")
class Calendar(Component):
    template_file = "template.html"

    def get_context_data(self, date):
        return {"date": date}

With this template.html file:

<div class="calendar-component">Today's date is <span>{{ date }}</span></div>

Use the component like this:

{% component "calendar" date="2024-11-06" %}{% endcomponent %}

And this is what gets rendered:

<div class="calendar-component">Today's date is <span>2024-11-06</span></div>

Read the full documentation

... or jump right into the code, check out the example project)

Release notes

Read the Release Notes to see the latest features and fixes.

Community examples

One of our goals with django-components is to make it easy to share components between projects. If you have a set of components that you think would be useful to others, please open a pull request to add them to the list below.

Contributing and development

Get involved or sponsor this project - See here

Running django-components locally for development - See here