django-components


Namedjango-components JSON
Version 0.61 PyPI version JSON
download
home_pagehttps://github.com/EmilStenstrom/django-components/
SummaryA way to create simple reusable template components in Django.
upload_time2024-03-24 11:12:08
maintainerNone
docs_urlNone
authorEmil Stenström
requires_pythonNone
licenseMIT
keywords django components css js html
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # django-components
<a href="https://github.com/EmilStenstrom/django-components/actions?query=workflow%3A%22Run+tests%22"><img align="right" src="https://github.com/EmilStenstrom/django-components/workflows/Run%20tests/badge.svg" alt="Show test status"></a>
<a href="https://pepy.tech/project/django-components"><img align="right" src="https://pepy.tech/badge/django-components" alt="Show download stats"></a>

A way to create simple reusable template components in Django.

It lets you create "template components", that contains both the template, the Javascript and the CSS needed to generate the front end code you need for a modern app. Components look like this:

```htmldjango
{% component "calendar" date="2015-06-19" %}{% endcomponent %}
```

And this is what gets rendered (plus the CSS and Javascript you've specified):

```html
<div class="calendar-component">Today's date is <span>2015-06-19</span></div>
```

Read on to learn about the details!

## Release notes

🚨📢 **Version 0.5** CHANGES THE SYNTAX for components. `component_block` is now `component`, and `component` blocks need an ending `endcomponent` tag. The new `python manage.py upgradecomponent` command can be used to upgrade a directory (use --path argument to point to each dir) of components to the new syntax automatically.

This change is done to simplify the API in anticipation of a 1.0 release of django_components. After 1.0 we intend to be stricter with big changes like this in point releases.

**Version 0.34** adds components as views, which allows you to handle requests and render responses from within a component. See the [documentation](#components-as-views) for more details.

**Version 0.28** introduces 'implicit' slot filling and the `default` option for `slot` tags.

**Version 0.27** adds a second installable app: *django_components.safer_staticfiles*. It provides the same behavior as *django.contrib.staticfiles* but with extra security guarantees (more info below in Security Notes).

**Version 0.26** changes the syntax for `{% slot %}` tags. From now on, we separate defining a slot (`{% slot %}`) from filling a slot with content (`{% fill %}`). This means you will likely need to change a lot of slot tags to fill. We understand this is annoying, but it's the only way we can get support for nested slots that fill in other slots, which is a very nice featuPpre to have access to. Hoping that this will feel worth it!

**Version 0.22** starts autoimporting all files inside components subdirectores, to simplify setup. An existing project might start to get AlreadyRegistered-errors because of this. To solve this, either remove your custom loading of components, or set "autodiscover": False in settings.COMPONENTS.

**Version 0.17** renames `Component.context` and `Component.template` to `get_context_data` and `get_template_name`. The old methods still work, but emit a deprecation warning. This change was done to sync naming with Django's class based views, and make using django-components more familiar to Django users. `Component.context` and `Component.template` will be removed when version 1.0 is released.

## Security notes 🚨

*You are advised to read this section before using django-components in production.*

### Static files

Components can be organized however you prefer.
That said, our prefered way is to keep the files of a component close together by bundling them in the same directory.
This means that files containing backend logic, such as Python modules and HTML templates, live in the same directory as static files, e.g. JS and CSS.

If your are using _django.contrib.staticfiles_ to collect static files, no distinction is made between the different kinds of files.
As a result, your Python code and templates may inadvertently become available on your static file server.
You probably don't want this, as parts of your backend logic will be exposed, posing a __potential security vulnerability__.

As of *v0.27*, django-components ships with an additional installable app *django_components.__safer_staticfiles__*.
It is a drop-in replacement for *django.contrib.staticfiles*.
Its behavior is 100% identical except it ignores .py and .html files, meaning these will not end up on your static files server.
To use it, add it to INSTALLED_APPS and remove _django.contrib.staticfiles_.

```python
INSTALLED_APPS = [
    # 'django.contrib.staticfiles',   # <-- REMOVE
    'django_components',
    'django_components.safer_staticfiles'  # <-- ADD
]
```

If you are on an older version of django-components, your alternatives are a) passing `--ignore <pattern>` options to the _collecstatic_ CLI command, or b) defining a subclass of StaticFilesConfig.
Both routes are described in the official [docs of the _staticfiles_ app](https://docs.djangoproject.com/en/4.2/ref/contrib/staticfiles/#customizing-the-ignored-pattern-list).

## Installation

Install the app into your environment:

> ```pip install django_components```

Then add the app into INSTALLED_APPS in settings.py

```python
INSTALLED_APPS = [
    ...,
    'django_components',
]
```

Modify `TEMPLATES` section of settings.py as follows:
- *Remove `'APP_DIRS': True,`*
- add `loaders` to `OPTIONS` list and set it to following value:

```python
TEMPLATES = [
    {
        ...,
        'OPTIONS': {
            'context_processors': [
                ...
            ],
            'loaders':[(
                'django.template.loaders.cached.Loader', [
                    'django.template.loaders.filesystem.Loader',
                    'django.template.loaders.app_directories.Loader',
                    'django_components.template_loader.Loader',
                ]
            )],
        },
    },
]
```

Modify STATICFILES_DIRS (or add it if you don't have it) so django can find your static JS and CSS files:

```python
STATICFILES_DIRS = [
    ...,
    os.path.join(BASE_DIR, "components"),
]
```

### Optional

To avoid loading the app in each template using ``` {% load django_components %} ```, you can add the tag as a 'builtin' in settings.py

```python
TEMPLATES = [
    {
        ...,
        'OPTIONS': {
            'context_processors': [
                ...
            ],
            'builtins': [
                'django_components.templatetags.component_tags',
            ]
        },
    },
]
```

Read on to find out how to build your first component!

## Compatiblity

Django-components supports all <a href="https://docs.djangoproject.com/en/dev/faq/install/#what-python-version-can-i-use-with-django">officially supported versions</a> of Django and Python.

| Python version | Django version           |
|----------------|--------------------------|
| 3.6            | 3.2                      |
| 3.7            | 3.2                      |
| 3.8            | 3.2, 4.0, 4.1, 4.2       |
| 3.9            | 3.2, 4.0, 4.1, 4.2       |
| 3.10           | 3.2, 4.0, 4.1, 4.2, 5.0  |
| 3.11           | 4.1, 4.2, 5.0            |
| 3.12           | 4.2, 5.0                 |

## Create your first component

A component in django-components is the combination of four things: CSS, Javascript, a Django template, and some Python code to put them all together.

![Directory structure for django_components](https://user-images.githubusercontent.com/224130/179460219-fb51eae1-aab2-4f69-b71f-90cd5ab51bb1.png)

Start by creating empty files in the structure above.

First you need a CSS file. Be sure to prefix all rules with a unique class so they don't clash with other rules.

```css
/* In a file called [project root]/components/calendar/style.css */
.calendar-component { width: 200px; background: pink; }
.calendar-component span { font-weight: bold; }
```

Then you need a javascript file that specifies how you interact with this component. You are free to use any javascript framework you want. A good way to make sure this component doesn't clash with other components is to define all code inside an anonymous function that calls itself. This makes all variables defined only be defined inside this component and not affect other components.

```js
/* In a file called [project root]/components/calendar/script.js */
(function(){
    if (document.querySelector(".calendar-component")) {
        document.querySelector(".calendar-component").onclick = function(){ alert("Clicked calendar!"); };
    }
})()
```

Now you need a Django template for your component. Feel free to define more variables like `date` in this example. When creating an instance of this component we will send in the values for these variables. The template will be rendered with whatever template backend you've specified in your Django settings file.

```htmldjango
{# In a file called [project root]/components/calendar/calendar.html #}
<div class="calendar-component">Today's date is <span>{{ date }}</span></div>
```

Finally, we use django-components to tie this together. Start by creating a file called `calendar.py` in your component calendar directory. It will be auto-detected and loaded by the app.

Inside this file we create a Component by inheriting from the Component class and specifying the context method. We also register the global component registry so that we easily can render it anywhere in our templates.

```python
# In a file called [project root]/components/calendar/calendar.py
from django_components import component

@component.register("calendar")
class Calendar(component.Component):
    # Templates inside `[your apps]/components` dir and `[project root]/components` dir will be automatically found. To customize which template to use based on context
    # you can override def get_template_name() instead of specifying the below variable.
    template_name = "calendar.html"

    # This component takes one parameter, a date string to show in the template
    def get_context_data(self, date):
        return {
            "date": date,
        }

    class Media:
        css = "style.css"
        js = "script.js"
```

And voilá!! We've created our first component.

## Use the component in a template

First load the `component_tags` tag library, then use the `component_[js/css]_dependencies` and `component` tags to render the component to the page.

```htmldjango
{% load component_tags %}
<!DOCTYPE html>
<html>
<head>
    <title>My example calendar</title>
    {% component_css_dependencies %}
</head>
<body>
    {% component "calendar" date="2015-06-19" %}{% endcomponent %}
    {% component_js_dependencies %}
</body>
<html>
```

The output from the above template will be:

```html
<!DOCTYPE html>
<html>
<head>
    <title>My example calendar</title>
    <link href="style.css" type="text/css" media="all" rel="stylesheet">
</head>
<body>
    <div class="calendar-component">Today's date is <span>2015-06-19</span></div>
    <script src="script.js"></script>
</body>
<html>
```

This makes it possible to organize your front-end around reusable components. Instead of relying on template tags and keeping your CSS and Javascript in the static directory.

## Using single-file components

Components can also be defined in a single file, which is useful for small components. To do this, you can use the `template`, `js`, and `css` class attributes instead of the `template_name` and `Media`. For example, here's the calendar component from above, defined in a single file:

```python
# In a file called [project root]/components/calendar.py
from django_components import component
from django_components import types as t 

@component.register("calendar")
class Calendar(component.Component):
    def get_context_data(self, date):
        return {
            "date": date,
        }
    
    template: t.django_html = """
        <div class="calendar-component">Today's date is <span>{{ date }}</span></div>
    """
    
    css: t.css = """
        .calendar-component { width: 200px; background: pink; }
        .calendar-component span { font-weight: bold; }
    """
    
    js: t.js = """
        (function(){
            if (document.querySelector(".calendar-component")) {
                document.querySelector(".calendar-component").onclick = function(){ alert("Clicked calendar!"); };
            }
        })()
    """
```

This makes it easy to create small components without having to create a separate template, CSS, and JS file.

Note that the `t.django_html`, `t.css`, and `t.js` types are used to specify the type of the template, CSS, and JS files, respectively. This is not necessary, but if you're using VSCode with the [Python Inline Source Syntax Highlighting](https://marketplace.visualstudio.com/items?itemName=samwillis.python-inline-source) extension, it will give you syntax highlighting for the template, CSS, and JS. 

## Using slots in templates

_New in version 0.26_:

- The `slot` tag now serves only to declare new slots inside the component template.
  - To override the content of a declared slot, use the newly introduced `fill` tag instead.
- Whereas unfilled slots used to raise a warning, filling a slot is now optional by default.
  - To indicate that a slot must be filled, the new `required` option should be added at the end of the `slot` tag.

---

Components support something called 'slots'.
When a component is used inside another template, slots allow the parent template to override specific parts of the child component by passing in different content.
This mechanism makes components more reusable and composable.

In the example below we introduce two block tags that work hand in hand to make this work. These are...

- `{% slot <name> %}`/`{% endslot %}`: Declares a new slot in the component template.
- `{% fill <name> %}`/`{% endfill %}`: (Used inside a `component` tag pair.) Fills a declared slot with the specified content.

Let's update our calendar component to support more customization. We'll add `slot` tag pairs to its template, _calendar.html_.

```htmldjango
<div class="calendar-component">
    <div class="header">
        {% slot "header" %}Calendar header{% endslot %}
    </div>
    <div class="body">
        {% slot "body" %}Today's date is <span>{{ date }}</span>{% endslot %}
    </div>
</div>
```

When using the component, you specify which slots you want to fill and where you want to use the defaults from the template. It looks like this:

```htmldjango
{% component "calendar" date="2020-06-06" %}
    {% fill "body" %}Can you believe it's already <span>{{ date }}</span>??{% endfill %}
{% endcomponent %}
```

Since the header block is unspecified, it's taken from the base template. If you put this in a template, and pass in `date=2020-06-06`, this is what gets rendered:

```htmldjango
<div class="calendar-component">
    <div class="header">
        Calendar header
    </div>
    <div class="body">
        Can you believe it's already <span>2020-06-06</span>??
    </div>
</div>
```

As you can see, component slots lets you write reusable containers that you fill in when you use a component. This makes for highly reusable components that can be used in different circumstances.

It can become tedious to use `fill` tags everywhere, especially when you're using a component that declares only one slot. To make things easier, `slot` tags can be marked with an optional keyword: `default`. When added to the end of the tag (as shown below), this option lets you pass filling content directly in the body of a `component` tag pair – without using a `fill` tag. Choose carefully, though: a component template may contain at most one slot that is marked as `default`. The `default` option can be combined with other slot options, e.g. `required`.

Here's the same example as before, except with default slots and implicit filling.

The template:

```htmldjango
<div class="calendar-component">
    <div class="header">
        {% slot "header" %}Calendar header{% endslot %}
    </div>
    <div class="body">
        {% slot "body" default %}Today's date is <span>{{ date }}</span>{% endslot %}
    </div>
</div>
```

Including the component (notice how the `fill` tag is omitted):

```htmldjango
{% component "calendar" date="2020-06-06" %}
    Can you believe it's already <span>{{ date }}</span>??
{% endcomponent %}
```

The rendered result (exactly the same as before):

```html
<div class="calendar-component">
    <div class="header">
        Calendar header
    </div>
    <div class="body">
        Can you believe it's already <span>2020-06-06</span>??
    </div>
</div>
```

You may be tempted to combine implicit fills with explicit `fill` tags. This will not work. The following component template will raise an error when compiled.

```htmldjango
{# DON'T DO THIS #}
{% component "calendar" date="2020-06-06" %}
    {% fill "header" %}Totally new header!{% endfill %}
    Can you believe it's already <span>{{ date }}</span>??
{% endcomponent %}
```

By contrast, it is permitted to use `fill` tags in nested components, e.g.:

```htmldjango
{% component "calendar" date="2020-06-06" %}
    {% component "beautiful-box" %}
        {% fill "content" %} Can you believe it's already <span>{{ date }}</span>?? {% endfill %}
    {% endcomponent %}
{% endcomponent %}
```

This is fine too:

```htmldjango
{% component "calendar" date="2020-06-06" %}
    {% fill "header" %}
        {% component "calendar-header" %}
            Super Special Calendar Header
        {% endcomponent %}
    {% endfill %}
{% endcomponent %}
```

### Components as views

_New in version 0.34_

Components can now be used as views. To do this, `Component` subclasses Django's `View` class. This means that you can use all of the [methods](https://docs.djangoproject.com/en/5.0/ref/class-based-views/base/#view) of `View` in your component. For example, you can override `get` and `post` to handle GET and POST requests, respectively. 

In addition, `Component` now has a `render_to_response` method that renders the component template based on the provided context and slots' data and returns an `HttpResponse` object.

Here's an example of a calendar component defined as a view:

```python
# In a file called [project root]/components/calendar.py
from django_components import component

@component.register("calendar")
class Calendar(component.Component):
    
    template = """
        <div class="calendar-component">
            <div class="header">
                {% slot "header" %}{% endslot %}
            </div>
            <div class="body">
                Today's date is <span>{{ date }}</span>
            </div>
        </div>
    """
    
    def get(self, request, *args, **kwargs):
        context = {
            "date": request.GET.get("date", "2020-06-06"),
        }
        slots = {
            "header": "Calendar header",
        }
        return self.render_to_response(context, slots)
```

Then, to use this component as a view, you should create a `urls.py` file in your components directory, and add a path to the component's view:

```python
# In a file called [project root]/components/urls.py
from django.urls import path
from calendar import Calendar 

urlpatterns = [
    path("calendar/", Calendar.as_view()),
]
```

Remember to add `__init__.py` to your components directory, so that Django can find the `urls.py` file.

Finally, include the component's urls in your project's `urls.py` file:

```python
# In a file called [project root]/urls.py
from django.urls import include, path

urlpatterns = [
    path("components/", include("components.urls")),
]
```

Note: slots content are automatically escaped by default to prevent XSS attacks. To disable escaping, set `escape_slots_content=False` in the `render_to_response` method. If you do so, you should make sure that any content you pass to the slots is safe, especially if it comes from user input.

If you're planning on passing an HTML string, check Django's use of [`format_html`](https://docs.djangoproject.com/en/5.0/ref/utils/#django.utils.html.format_html) and [`mark_safe`](https://docs.djangoproject.com/en/5.0/ref/utils/#django.utils.safestring.mark_safe).

### Advanced

#### Re-using content defined in the original slot

Certain properties of a slot can be accessed from within a 'fill' context. They are provided as attributes on a user-defined alias of the targeted slot. For instance, let's say you're filling a slot called 'body'. To access properties of this slot, alias it using the 'as' keyword to a new name -- or keep the original name. With the new slot alias, you can call `<alias>.default` to insert the default content.

```htmldjango
{% component "calendar" date="2020-06-06" %}
    {% fill "body" as "body" %}{{ body.default }}. Have a great day!{% endfill %}
{% endcomponent %}
```

Produces:

```htmldjango
<div class="calendar-component">
    <div class="header">
        Calendar header
    </div>
    <div class="body">
        Today's date is <span>2020-06-06</span>. Have a great day!
    </div>
</div>
```


#### Conditional slots

_Added in version 0.26._

In certain circumstances, you may want the behavior of slot filling to depend on
whether or not a particular slot is filled.

For example, suppose we have the following component template:

```htmldjango
<div class="frontmatter-component">
    <div class="title">
        {% slot "title" %}Title{% endslot %}
    </div>
    <div class="subtitle">
        {% slot "subtitle" %}{# Optional subtitle #}{% endslot %}
    </div>
</div>
```

By default the slot named 'subtitle' is empty. Yet when the component is used without
explicit fills, the div containing the slot is still rendered, as shown below:

```html
<div class="frontmatter-component">
    <div class="title">
        Title
    </div>
    <div class="subtitle">
    </div>
</div>
```

This may not be what you want. What if instead the outer 'subtitle' div should only
be included when the inner slot is in fact filled?

The answer is to use the `{% if_filled <name> %}` tag. Together with `{% endif_filled %}`,
these define a block whose contents will be rendered only if the component slot with
the corresponding 'name' is filled.

This is what our example looks like with an 'if_filled' tag.

```htmldjango
<div class="frontmatter-component">
    <div class="title">
        {% slot "title" %}Title{% endslot %}
    </div>
    {% if_filled "subtitle" %}
    <div class="subtitle">
        {% slot "subtitle" %}{# Optional subtitle #}{% endslot %}
    </div>
    {% endif_filled %}
</div>
```

Just as Django's builtin 'if' tag has 'elif' and 'else' counterparts, so does 'if_filled'
include additional tags for more complex branching. These tags are 'elif_filled' and
'else_filled'. Here's what our example looks like with them.

```htmldjango
<div class="frontmatter-component">
    <div class="title">
        {% slot "title" %}Title{% endslot %}
    </div>
    {% if_filled "subtitle" %}
    <div class="subtitle">
        {% slot "subtitle" %}{# Optional subtitle #}{% endslot %}
    </div>
    {% elif_filled "title" %}
        ...
    {% else_filled %}
        ...
    {% endif_filled %}
</div>
```

Sometimes you're not interested in whether a slot is filled, but rather that it _isn't_.
To negate the meaning of 'if_filled' in this way, an optional boolean can be passed to
the 'if_filled' and 'elif_filled' tags.

In the example below we use `False` to indicate that the content should be rendered
only if the slot 'subtitle' is _not_ filled.

```htmldjango
{% if_filled subtitle False %}
<div class="subtitle">
    {% slot "subtitle" %}{% endslot %}
</div>
{% endif_filled %}
```

### Setting Up `ComponentDependencyMiddleware`

`ComponentDependencyMiddleware` is a Django middleware designed to manage and inject CSS/JS dependencies for rendered components dynamically. It ensures that only the necessary stylesheets and scripts are loaded in your HTML responses, based on the components used in your Django templates.

To set it up, add the middleware to your `MIDDLEWARE` in settings.py:

```python
MIDDLEWARE = [
    # ... other middleware classes ...
    'django_components.middleware.ComponentDependencyMiddleware'
    # ... other middleware classes ...
]
```

Then, enable `RENDER_DEPENDENCIES` in setting.py:

```python
COMPONENTS = {
    "RENDER_DEPENDENCIES": True,
    # ... other component settings ...
}
```

## Component context and scope

By default, components can access context variables from the parent template, just like templates that are included with the `{% include %}` tag. Just like with `{% include %}`, if you don't want the component template to have access to the parent context, add `only` to the end of the `{% component %}` tag):

```htmldjango
   {% component "calendar" date="2015-06-19" only %}{% endcomponent %}
```

NOTE: `{% csrf_token %}` tags need access to the top-level context, and they will not function properly if they are rendered in a component that is called with the `only` modifier.

Components can also access the outer context in their context methods by accessing the property `outer_context`.

You can also set `context_behavior` to `isolated` to make all components isolated by default. This is useful if you want to make sure that components don't accidentally access the outer context.

## Available settings

All library settings are handled from a global COMPONENTS variable that is read from settings.py. By default you don't need it set, there are resonable defaults.

### Configure the module where components are loaded from

Configure the location where components are loaded. To do this, add a COMPONENTS variable to you settings.py with a list of python paths to load. This allows you to build a structure of components that are independent from your apps.

```python
COMPONENTS = {
    "libraries": [
        "mysite.components.forms",
        "mysite.components.buttons",
        "mysite.components.cards",
    ],
}
```

### Disable autodiscovery

If you specify all the component locations with the setting above and have a lot of apps, you can (very) slightly speed things up by disabling autodiscovery.

```python
COMPONENTS = {
    "autodiscover": False,
}
```

### Tune the template cache

Each time a template is rendered it is cached to a global in-memory cache (using Python's lru_cache decorator). This speeds up the next render of the component. As the same component is often used many times on the same page, these savings add up. By default the cache holds 128 component templates in memory, which should be enough for most sites. But if you have a lot of components, or if you are using the `template` method of a component to render lots of dynamic templates, you can increase this number. To remove the cache limit altogether and cache everything, set template_cache_size to `None`.

```python
COMPONENTS = {
    "template_cache_size": 256,
}
```

### Isolate components' context by default

If you'd like to prevent components from accessing the outer context by default, you can set the `context_behavior` setting to `isolated`. This is useful if you want to make sure that components don't accidentally access the outer context.

```python
COMPONENTS = {
    "context_behavior": "isolated",
}
```

## Logging and debugging

Django components supports [logging with Django](https://docs.djangoproject.com/en/5.0/howto/logging/#logging-how-to). This can help with troubleshooting.

To configure logging for Django components, set the `django_components` logger in `LOGGING` in `settings.py` (below).

Also see the [`settings.py` file in sampleproject](./sampleproject/sampleproject/settings.py) for a real-life example.

```py
import logging
import sys

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    "handlers": {
        "console": {
            'class': 'logging.StreamHandler',
            'stream': sys.stdout,
        },
    },
    "loggers": {
        "django_components": {
            "level": logging.DEBUG,
            "handlers": ["console"],
        },
    },
}
```

## Management Command

You can use the built-in management command `startcomponent` to create a django component. The command accepts the following arguments and options:

- `name`: The name of the component to create. This is a required argument.

- `--path`: The path to the components directory. This is an optional argument. If not provided, the command will use the `BASE_DIR` setting from your Django settings.

- `--js`: The name of the JavaScript file. This is an optional argument. The default value is `script.js`.

- `--css`: The name of the CSS file. This is an optional argument. The default value is `style.css`.

- `--template`: The name of the template file. This is an optional argument. The default value is `template.html`.

- `--force`: This option allows you to overwrite existing files if they exist. This is an optional argument.

- `--verbose`: This option allows the command to print additional information during component creation. This is an optional argument.

- `--dry-run`: This option allows you to simulate component creation without actually creating any files. This is an optional argument. The default value is `False`.

### Management Command Usage

To use the command, run the following command in your terminal:

```bash
python manage.py startcomponent <name> --path <path> --js <js_filename> --css <css_filename> --template <template_filename> --force --verbose --dry-run
```

Replace `<name>`, `<path>`, `<js_filename>`, `<css_filename>`, and `<template_filename>` with your desired values.

### Management Command Examples

Here are some examples of how you can use the command:

### Creating a Component with Default Settings

To create a component with the default settings, you only need to provide the name of the component:

```bash
python manage.py startcomponent my_component
```

This will create a new component named `my_component` in the `components` directory of your Django project. The JavaScript, CSS, and template files will be named `script.js`, `style.css`, and `template.html`, respectively.

### Creating a Component with Custom Settings

You can also create a component with custom settings by providing additional arguments:

```bash
python manage.py startcomponent new_component --path my_components --js my_script.js --css my_style.css --template my_template.html
```

This will create a new component named `new_component` in the `my_components` directory. The JavaScript, CSS, and template files will be named `my_script.js`, `my_style.css`, and `my_template.html`, respectively.

### Overwriting an Existing Component

If you want to overwrite an existing component, you can use the `--force` option:

```bash
python manage.py startcomponent my_component --force
```

This will overwrite the existing `my_component` if it exists.

### Simulating Component Creation

If you want to simulate the creation of a component without actually creating any files, you can use the `--dry-run` option:

```bash
python manage.py startcomponent my_component --dry-run
```

This will simulate the creation of `my_component` without creating any files.

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

- [django-htmx-components](https://github.com/iwanalabs/django-htmx-components): A set of components for use with [htmx](https://htmx.org/). Try out the [live demo](https://dhc.iwanalabs.com/).


## Running django-components project locally

### Install locally and run the tests

Start by forking the project by clicking the **Fork button** up in the right corner in the GitHub . This makes a copy of the repository in your own name. Now you can clone this repository locally and start adding features:

```sh
git clone https://github.com/<your GitHub username>/django-components.git
```

To quickly run the tests install the local dependencies by running:

```sh
pip install -r requirements-dev.txt
```

Now you can run the tests to make sure everything works as expected:

```sh
pytest
```

The library is also tested across many versions of Python and Django. To run tests that way:

```sh
pyenv install -s 3.6
pyenv install -s 3.7
pyenv install -s 3.8
pyenv install -s 3.9
pyenv install -s 3.10
pyenv install -s 3.11
pyenv install -s 3.12
pyenv local 3.6 3.7 3.8 3.9 3.10 3.11 3.12
tox -p
```

### Developing against live Django app

How do you check that your changes to django-components project will work in an actual Django project?

Use the [sampleproject](./sampleproject/) demo project to validate the changes:

1. Navigate to [sampleproject](./sampleproject/) directory:
    ```sh
    cd sampleproject
    ```

2. Install dependencies from the [requirements.txt](./sampleproject/requirements.txt) file:
    ```sh
    pip install -r requirements.txt
    ```

3. Link to your local version of django-components:
    ```sh
    pip install -e ..
    ```
    NOTE: The path (in this case `..`) must point to the directory that has the `setup.py` file.

4. Start Django server
    ```sh
    python manage.py runserver
    ```

Once the server is up, it should be available at <http://127.0.0.1:8000>.

To display individual components, add them to the `urls.py`, like in the case of <http://127.0.0.1:8000/greeting>

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/EmilStenstrom/django-components/",
    "name": "django-components",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "django, components, css, js, html",
    "author": "Emil Stenstr\u00f6m",
    "author_email": "emil@emilstenstrom.se",
    "download_url": "https://files.pythonhosted.org/packages/f6/5f/1947168d32485f0eb313de3a27fb89f45f61ba5650266310918e4ad4f0b2/django_components-0.61.tar.gz",
    "platform": null,
    "description": "# django-components\n<a href=\"https://github.com/EmilStenstrom/django-components/actions?query=workflow%3A%22Run+tests%22\"><img align=\"right\" src=\"https://github.com/EmilStenstrom/django-components/workflows/Run%20tests/badge.svg\" alt=\"Show test status\"></a>\n<a href=\"https://pepy.tech/project/django-components\"><img align=\"right\" src=\"https://pepy.tech/badge/django-components\" alt=\"Show download stats\"></a>\n\nA way to create simple reusable template components in Django.\n\nIt lets you create \"template components\", that contains both the template, the Javascript and the CSS needed to generate the front end code you need for a modern app. Components look like this:\n\n```htmldjango\n{% component \"calendar\" date=\"2015-06-19\" %}{% endcomponent %}\n```\n\nAnd this is what gets rendered (plus the CSS and Javascript you've specified):\n\n```html\n<div class=\"calendar-component\">Today's date is <span>2015-06-19</span></div>\n```\n\nRead on to learn about the details!\n\n## Release notes\n\n\ud83d\udea8\ud83d\udce2 **Version 0.5** CHANGES THE SYNTAX for components. `component_block` is now `component`, and `component` blocks need an ending `endcomponent` tag. The new `python manage.py upgradecomponent` command can be used to upgrade a directory (use --path argument to point to each dir) of components to the new syntax automatically.\n\nThis change is done to simplify the API in anticipation of a 1.0 release of django_components. After 1.0 we intend to be stricter with big changes like this in point releases.\n\n**Version 0.34** adds components as views, which allows you to handle requests and render responses from within a component. See the [documentation](#components-as-views) for more details.\n\n**Version 0.28** introduces 'implicit' slot filling and the `default` option for `slot` tags.\n\n**Version 0.27** adds a second installable app: *django_components.safer_staticfiles*. It provides the same behavior as *django.contrib.staticfiles* but with extra security guarantees (more info below in Security Notes).\n\n**Version 0.26** changes the syntax for `{% slot %}` tags. From now on, we separate defining a slot (`{% slot %}`) from filling a slot with content (`{% fill %}`). This means you will likely need to change a lot of slot tags to fill. We understand this is annoying, but it's the only way we can get support for nested slots that fill in other slots, which is a very nice featuPpre to have access to. Hoping that this will feel worth it!\n\n**Version 0.22** starts autoimporting all files inside components subdirectores, to simplify setup. An existing project might start to get AlreadyRegistered-errors because of this. To solve this, either remove your custom loading of components, or set \"autodiscover\": False in settings.COMPONENTS.\n\n**Version 0.17** renames `Component.context` and `Component.template` to `get_context_data` and `get_template_name`. The old methods still work, but emit a deprecation warning. This change was done to sync naming with Django's class based views, and make using django-components more familiar to Django users. `Component.context` and `Component.template` will be removed when version 1.0 is released.\n\n## Security notes \ud83d\udea8\n\n*You are advised to read this section before using django-components in production.*\n\n### Static files\n\nComponents can be organized however you prefer.\nThat said, our prefered way is to keep the files of a component close together by bundling them in the same directory.\nThis means that files containing backend logic, such as Python modules and HTML templates, live in the same directory as static files, e.g. JS and CSS.\n\nIf your are using _django.contrib.staticfiles_ to collect static files, no distinction is made between the different kinds of files.\nAs a result, your Python code and templates may inadvertently become available on your static file server.\nYou probably don't want this, as parts of your backend logic will be exposed, posing a __potential security vulnerability__.\n\nAs of *v0.27*, django-components ships with an additional installable app *django_components.__safer_staticfiles__*.\nIt is a drop-in replacement for *django.contrib.staticfiles*.\nIts behavior is 100% identical except it ignores .py and .html files, meaning these will not end up on your static files server.\nTo use it, add it to INSTALLED_APPS and remove _django.contrib.staticfiles_.\n\n```python\nINSTALLED_APPS = [\n    # 'django.contrib.staticfiles',   # <-- REMOVE\n    'django_components',\n    'django_components.safer_staticfiles'  # <-- ADD\n]\n```\n\nIf you are on an older version of django-components, your alternatives are a) passing `--ignore <pattern>` options to the _collecstatic_ CLI command, or b) defining a subclass of StaticFilesConfig.\nBoth routes are described in the official [docs of the _staticfiles_ app](https://docs.djangoproject.com/en/4.2/ref/contrib/staticfiles/#customizing-the-ignored-pattern-list).\n\n## Installation\n\nInstall the app into your environment:\n\n> ```pip install django_components```\n\nThen add the app into INSTALLED_APPS in settings.py\n\n```python\nINSTALLED_APPS = [\n    ...,\n    'django_components',\n]\n```\n\nModify `TEMPLATES` section of settings.py as follows:\n- *Remove `'APP_DIRS': True,`*\n- add `loaders` to `OPTIONS` list and set it to following value:\n\n```python\nTEMPLATES = [\n    {\n        ...,\n        'OPTIONS': {\n            'context_processors': [\n                ...\n            ],\n            'loaders':[(\n                'django.template.loaders.cached.Loader', [\n                    'django.template.loaders.filesystem.Loader',\n                    'django.template.loaders.app_directories.Loader',\n                    'django_components.template_loader.Loader',\n                ]\n            )],\n        },\n    },\n]\n```\n\nModify STATICFILES_DIRS (or add it if you don't have it) so django can find your static JS and CSS files:\n\n```python\nSTATICFILES_DIRS = [\n    ...,\n    os.path.join(BASE_DIR, \"components\"),\n]\n```\n\n### Optional\n\nTo avoid loading the app in each template using ``` {% load django_components %} ```, you can add the tag as a 'builtin' in settings.py\n\n```python\nTEMPLATES = [\n    {\n        ...,\n        'OPTIONS': {\n            'context_processors': [\n                ...\n            ],\n            'builtins': [\n                'django_components.templatetags.component_tags',\n            ]\n        },\n    },\n]\n```\n\nRead on to find out how to build your first component!\n\n## Compatiblity\n\nDjango-components supports all <a href=\"https://docs.djangoproject.com/en/dev/faq/install/#what-python-version-can-i-use-with-django\">officially supported versions</a> of Django and Python.\n\n| Python version | Django version           |\n|----------------|--------------------------|\n| 3.6            | 3.2                      |\n| 3.7            | 3.2                      |\n| 3.8            | 3.2, 4.0, 4.1, 4.2       |\n| 3.9            | 3.2, 4.0, 4.1, 4.2       |\n| 3.10           | 3.2, 4.0, 4.1, 4.2, 5.0  |\n| 3.11           | 4.1, 4.2, 5.0            |\n| 3.12           | 4.2, 5.0                 |\n\n## Create your first component\n\nA component in django-components is the combination of four things: CSS, Javascript, a Django template, and some Python code to put them all together.\n\n![Directory structure for django_components](https://user-images.githubusercontent.com/224130/179460219-fb51eae1-aab2-4f69-b71f-90cd5ab51bb1.png)\n\nStart by creating empty files in the structure above.\n\nFirst you need a CSS file. Be sure to prefix all rules with a unique class so they don't clash with other rules.\n\n```css\n/* In a file called [project root]/components/calendar/style.css */\n.calendar-component { width: 200px; background: pink; }\n.calendar-component span { font-weight: bold; }\n```\n\nThen you need a javascript file that specifies how you interact with this component. You are free to use any javascript framework you want. A good way to make sure this component doesn't clash with other components is to define all code inside an anonymous function that calls itself. This makes all variables defined only be defined inside this component and not affect other components.\n\n```js\n/* In a file called [project root]/components/calendar/script.js */\n(function(){\n    if (document.querySelector(\".calendar-component\")) {\n        document.querySelector(\".calendar-component\").onclick = function(){ alert(\"Clicked calendar!\"); };\n    }\n})()\n```\n\nNow you need a Django template for your component. Feel free to define more variables like `date` in this example. When creating an instance of this component we will send in the values for these variables. The template will be rendered with whatever template backend you've specified in your Django settings file.\n\n```htmldjango\n{# In a file called [project root]/components/calendar/calendar.html #}\n<div class=\"calendar-component\">Today's date is <span>{{ date }}</span></div>\n```\n\nFinally, we use django-components to tie this together. Start by creating a file called `calendar.py` in your component calendar directory. It will be auto-detected and loaded by the app.\n\nInside this file we create a Component by inheriting from the Component class and specifying the context method. We also register the global component registry so that we easily can render it anywhere in our templates.\n\n```python\n# In a file called [project root]/components/calendar/calendar.py\nfrom django_components import component\n\n@component.register(\"calendar\")\nclass Calendar(component.Component):\n    # Templates inside `[your apps]/components` dir and `[project root]/components` dir will be automatically found. To customize which template to use based on context\n    # you can override def get_template_name() instead of specifying the below variable.\n    template_name = \"calendar.html\"\n\n    # This component takes one parameter, a date string to show in the template\n    def get_context_data(self, date):\n        return {\n            \"date\": date,\n        }\n\n    class Media:\n        css = \"style.css\"\n        js = \"script.js\"\n```\n\nAnd voil\u00e1!! We've created our first component.\n\n## Use the component in a template\n\nFirst load the `component_tags` tag library, then use the `component_[js/css]_dependencies` and `component` tags to render the component to the page.\n\n```htmldjango\n{% load component_tags %}\n<!DOCTYPE html>\n<html>\n<head>\n    <title>My example calendar</title>\n    {% component_css_dependencies %}\n</head>\n<body>\n    {% component \"calendar\" date=\"2015-06-19\" %}{% endcomponent %}\n    {% component_js_dependencies %}\n</body>\n<html>\n```\n\nThe output from the above template will be:\n\n```html\n<!DOCTYPE html>\n<html>\n<head>\n    <title>My example calendar</title>\n    <link href=\"style.css\" type=\"text/css\" media=\"all\" rel=\"stylesheet\">\n</head>\n<body>\n    <div class=\"calendar-component\">Today's date is <span>2015-06-19</span></div>\n    <script src=\"script.js\"></script>\n</body>\n<html>\n```\n\nThis makes it possible to organize your front-end around reusable components. Instead of relying on template tags and keeping your CSS and Javascript in the static directory.\n\n## Using single-file components\n\nComponents can also be defined in a single file, which is useful for small components. To do this, you can use the `template`, `js`, and `css` class attributes instead of the `template_name` and `Media`. For example, here's the calendar component from above, defined in a single file:\n\n```python\n# In a file called [project root]/components/calendar.py\nfrom django_components import component\nfrom django_components import types as t \n\n@component.register(\"calendar\")\nclass Calendar(component.Component):\n    def get_context_data(self, date):\n        return {\n            \"date\": date,\n        }\n    \n    template: t.django_html = \"\"\"\n        <div class=\"calendar-component\">Today's date is <span>{{ date }}</span></div>\n    \"\"\"\n    \n    css: t.css = \"\"\"\n        .calendar-component { width: 200px; background: pink; }\n        .calendar-component span { font-weight: bold; }\n    \"\"\"\n    \n    js: t.js = \"\"\"\n        (function(){\n            if (document.querySelector(\".calendar-component\")) {\n                document.querySelector(\".calendar-component\").onclick = function(){ alert(\"Clicked calendar!\"); };\n            }\n        })()\n    \"\"\"\n```\n\nThis makes it easy to create small components without having to create a separate template, CSS, and JS file.\n\nNote that the `t.django_html`, `t.css`, and `t.js` types are used to specify the type of the template, CSS, and JS files, respectively. This is not necessary, but if you're using VSCode with the [Python Inline Source Syntax Highlighting](https://marketplace.visualstudio.com/items?itemName=samwillis.python-inline-source) extension, it will give you syntax highlighting for the template, CSS, and JS. \n\n## Using slots in templates\n\n_New in version 0.26_:\n\n- The `slot` tag now serves only to declare new slots inside the component template.\n  - To override the content of a declared slot, use the newly introduced `fill` tag instead.\n- Whereas unfilled slots used to raise a warning, filling a slot is now optional by default.\n  - To indicate that a slot must be filled, the new `required` option should be added at the end of the `slot` tag.\n\n---\n\nComponents support something called 'slots'.\nWhen a component is used inside another template, slots allow the parent template to override specific parts of the child component by passing in different content.\nThis mechanism makes components more reusable and composable.\n\nIn the example below we introduce two block tags that work hand in hand to make this work. These are...\n\n- `{% slot <name> %}`/`{% endslot %}`: Declares a new slot in the component template.\n- `{% fill <name> %}`/`{% endfill %}`: (Used inside a `component` tag pair.) Fills a declared slot with the specified content.\n\nLet's update our calendar component to support more customization. We'll add `slot` tag pairs to its template, _calendar.html_.\n\n```htmldjango\n<div class=\"calendar-component\">\n    <div class=\"header\">\n        {% slot \"header\" %}Calendar header{% endslot %}\n    </div>\n    <div class=\"body\">\n        {% slot \"body\" %}Today's date is <span>{{ date }}</span>{% endslot %}\n    </div>\n</div>\n```\n\nWhen using the component, you specify which slots you want to fill and where you want to use the defaults from the template. It looks like this:\n\n```htmldjango\n{% component \"calendar\" date=\"2020-06-06\" %}\n    {% fill \"body\" %}Can you believe it's already <span>{{ date }}</span>??{% endfill %}\n{% endcomponent %}\n```\n\nSince the header block is unspecified, it's taken from the base template. If you put this in a template, and pass in `date=2020-06-06`, this is what gets rendered:\n\n```htmldjango\n<div class=\"calendar-component\">\n    <div class=\"header\">\n        Calendar header\n    </div>\n    <div class=\"body\">\n        Can you believe it's already <span>2020-06-06</span>??\n    </div>\n</div>\n```\n\nAs you can see, component slots lets you write reusable containers that you fill in when you use a component. This makes for highly reusable components that can be used in different circumstances.\n\nIt can become tedious to use `fill` tags everywhere, especially when you're using a component that declares only one slot. To make things easier, `slot` tags can be marked with an optional keyword: `default`. When added to the end of the tag (as shown below), this option lets you pass filling content directly in the body of a `component` tag pair \u2013 without using a `fill` tag. Choose carefully, though: a component template may contain at most one slot that is marked as `default`. The `default` option can be combined with other slot options, e.g. `required`.\n\nHere's the same example as before, except with default slots and implicit filling.\n\nThe template:\n\n```htmldjango\n<div class=\"calendar-component\">\n    <div class=\"header\">\n        {% slot \"header\" %}Calendar header{% endslot %}\n    </div>\n    <div class=\"body\">\n        {% slot \"body\" default %}Today's date is <span>{{ date }}</span>{% endslot %}\n    </div>\n</div>\n```\n\nIncluding the component (notice how the `fill` tag is omitted):\n\n```htmldjango\n{% component \"calendar\" date=\"2020-06-06\" %}\n    Can you believe it's already <span>{{ date }}</span>??\n{% endcomponent %}\n```\n\nThe rendered result (exactly the same as before):\n\n```html\n<div class=\"calendar-component\">\n    <div class=\"header\">\n        Calendar header\n    </div>\n    <div class=\"body\">\n        Can you believe it's already <span>2020-06-06</span>??\n    </div>\n</div>\n```\n\nYou may be tempted to combine implicit fills with explicit `fill` tags. This will not work. The following component template will raise an error when compiled.\n\n```htmldjango\n{# DON'T DO THIS #}\n{% component \"calendar\" date=\"2020-06-06\" %}\n    {% fill \"header\" %}Totally new header!{% endfill %}\n    Can you believe it's already <span>{{ date }}</span>??\n{% endcomponent %}\n```\n\nBy contrast, it is permitted to use `fill` tags in nested components, e.g.:\n\n```htmldjango\n{% component \"calendar\" date=\"2020-06-06\" %}\n    {% component \"beautiful-box\" %}\n        {% fill \"content\" %} Can you believe it's already <span>{{ date }}</span>?? {% endfill %}\n    {% endcomponent %}\n{% endcomponent %}\n```\n\nThis is fine too:\n\n```htmldjango\n{% component \"calendar\" date=\"2020-06-06\" %}\n    {% fill \"header\" %}\n        {% component \"calendar-header\" %}\n            Super Special Calendar Header\n        {% endcomponent %}\n    {% endfill %}\n{% endcomponent %}\n```\n\n### Components as views\n\n_New in version 0.34_\n\nComponents can now be used as views. To do this, `Component` subclasses Django's `View` class. This means that you can use all of the [methods](https://docs.djangoproject.com/en/5.0/ref/class-based-views/base/#view) of `View` in your component. For example, you can override `get` and `post` to handle GET and POST requests, respectively. \n\nIn addition, `Component` now has a `render_to_response` method that renders the component template based on the provided context and slots' data and returns an `HttpResponse` object.\n\nHere's an example of a calendar component defined as a view:\n\n```python\n# In a file called [project root]/components/calendar.py\nfrom django_components import component\n\n@component.register(\"calendar\")\nclass Calendar(component.Component):\n    \n    template = \"\"\"\n        <div class=\"calendar-component\">\n            <div class=\"header\">\n                {% slot \"header\" %}{% endslot %}\n            </div>\n            <div class=\"body\">\n                Today's date is <span>{{ date }}</span>\n            </div>\n        </div>\n    \"\"\"\n    \n    def get(self, request, *args, **kwargs):\n        context = {\n            \"date\": request.GET.get(\"date\", \"2020-06-06\"),\n        }\n        slots = {\n            \"header\": \"Calendar header\",\n        }\n        return self.render_to_response(context, slots)\n```\n\nThen, to use this component as a view, you should create a `urls.py` file in your components directory, and add a path to the component's view:\n\n```python\n# In a file called [project root]/components/urls.py\nfrom django.urls import path\nfrom calendar import Calendar \n\nurlpatterns = [\n    path(\"calendar/\", Calendar.as_view()),\n]\n```\n\nRemember to add `__init__.py` to your components directory, so that Django can find the `urls.py` file.\n\nFinally, include the component's urls in your project's `urls.py` file:\n\n```python\n# In a file called [project root]/urls.py\nfrom django.urls import include, path\n\nurlpatterns = [\n    path(\"components/\", include(\"components.urls\")),\n]\n```\n\nNote: slots content are automatically escaped by default to prevent XSS attacks. To disable escaping, set `escape_slots_content=False` in the `render_to_response` method. If you do so, you should make sure that any content you pass to the slots is safe, especially if it comes from user input.\n\nIf you're planning on passing an HTML string, check Django's use of [`format_html`](https://docs.djangoproject.com/en/5.0/ref/utils/#django.utils.html.format_html) and [`mark_safe`](https://docs.djangoproject.com/en/5.0/ref/utils/#django.utils.safestring.mark_safe).\n\n### Advanced\n\n#### Re-using content defined in the original slot\n\nCertain properties of a slot can be accessed from within a 'fill' context. They are provided as attributes on a user-defined alias of the targeted slot. For instance, let's say you're filling a slot called 'body'. To access properties of this slot, alias it using the 'as' keyword to a new name -- or keep the original name. With the new slot alias, you can call `<alias>.default` to insert the default content.\n\n```htmldjango\n{% component \"calendar\" date=\"2020-06-06\" %}\n    {% fill \"body\" as \"body\" %}{{ body.default }}. Have a great day!{% endfill %}\n{% endcomponent %}\n```\n\nProduces:\n\n```htmldjango\n<div class=\"calendar-component\">\n    <div class=\"header\">\n        Calendar header\n    </div>\n    <div class=\"body\">\n        Today's date is <span>2020-06-06</span>. Have a great day!\n    </div>\n</div>\n```\n\n\n#### Conditional slots\n\n_Added in version 0.26._\n\nIn certain circumstances, you may want the behavior of slot filling to depend on\nwhether or not a particular slot is filled.\n\nFor example, suppose we have the following component template:\n\n```htmldjango\n<div class=\"frontmatter-component\">\n    <div class=\"title\">\n        {% slot \"title\" %}Title{% endslot %}\n    </div>\n    <div class=\"subtitle\">\n        {% slot \"subtitle\" %}{# Optional subtitle #}{% endslot %}\n    </div>\n</div>\n```\n\nBy default the slot named 'subtitle' is empty. Yet when the component is used without\nexplicit fills, the div containing the slot is still rendered, as shown below:\n\n```html\n<div class=\"frontmatter-component\">\n    <div class=\"title\">\n        Title\n    </div>\n    <div class=\"subtitle\">\n    </div>\n</div>\n```\n\nThis may not be what you want. What if instead the outer 'subtitle' div should only\nbe included when the inner slot is in fact filled?\n\nThe answer is to use the `{% if_filled <name> %}` tag. Together with `{% endif_filled %}`,\nthese define a block whose contents will be rendered only if the component slot with\nthe corresponding 'name' is filled.\n\nThis is what our example looks like with an 'if_filled' tag.\n\n```htmldjango\n<div class=\"frontmatter-component\">\n    <div class=\"title\">\n        {% slot \"title\" %}Title{% endslot %}\n    </div>\n    {% if_filled \"subtitle\" %}\n    <div class=\"subtitle\">\n        {% slot \"subtitle\" %}{# Optional subtitle #}{% endslot %}\n    </div>\n    {% endif_filled %}\n</div>\n```\n\nJust as Django's builtin 'if' tag has 'elif' and 'else' counterparts, so does 'if_filled'\ninclude additional tags for more complex branching. These tags are 'elif_filled' and\n'else_filled'. Here's what our example looks like with them.\n\n```htmldjango\n<div class=\"frontmatter-component\">\n    <div class=\"title\">\n        {% slot \"title\" %}Title{% endslot %}\n    </div>\n    {% if_filled \"subtitle\" %}\n    <div class=\"subtitle\">\n        {% slot \"subtitle\" %}{# Optional subtitle #}{% endslot %}\n    </div>\n    {% elif_filled \"title\" %}\n        ...\n    {% else_filled %}\n        ...\n    {% endif_filled %}\n</div>\n```\n\nSometimes you're not interested in whether a slot is filled, but rather that it _isn't_.\nTo negate the meaning of 'if_filled' in this way, an optional boolean can be passed to\nthe 'if_filled' and 'elif_filled' tags.\n\nIn the example below we use `False` to indicate that the content should be rendered\nonly if the slot 'subtitle' is _not_ filled.\n\n```htmldjango\n{% if_filled subtitle False %}\n<div class=\"subtitle\">\n    {% slot \"subtitle\" %}{% endslot %}\n</div>\n{% endif_filled %}\n```\n\n### Setting Up `ComponentDependencyMiddleware`\n\n`ComponentDependencyMiddleware` is a Django middleware designed to manage and inject CSS/JS dependencies for rendered components dynamically. It ensures that only the necessary stylesheets and scripts are loaded in your HTML responses, based on the components used in your Django templates.\n\nTo set it up, add the middleware to your `MIDDLEWARE` in settings.py:\n\n```python\nMIDDLEWARE = [\n    # ... other middleware classes ...\n    'django_components.middleware.ComponentDependencyMiddleware'\n    # ... other middleware classes ...\n]\n```\n\nThen, enable `RENDER_DEPENDENCIES` in setting.py:\n\n```python\nCOMPONENTS = {\n    \"RENDER_DEPENDENCIES\": True,\n    # ... other component settings ...\n}\n```\n\n## Component context and scope\n\nBy default, components can access context variables from the parent template, just like templates that are included with the `{% include %}` tag. Just like with `{% include %}`, if you don't want the component template to have access to the parent context, add `only` to the end of the `{% component %}` tag):\n\n```htmldjango\n   {% component \"calendar\" date=\"2015-06-19\" only %}{% endcomponent %}\n```\n\nNOTE: `{% csrf_token %}` tags need access to the top-level context, and they will not function properly if they are rendered in a component that is called with the `only` modifier.\n\nComponents can also access the outer context in their context methods by accessing the property `outer_context`.\n\nYou can also set `context_behavior` to `isolated` to make all components isolated by default. This is useful if you want to make sure that components don't accidentally access the outer context.\n\n## Available settings\n\nAll library settings are handled from a global COMPONENTS variable that is read from settings.py. By default you don't need it set, there are resonable defaults.\n\n### Configure the module where components are loaded from\n\nConfigure the location where components are loaded. To do this, add a COMPONENTS variable to you settings.py with a list of python paths to load. This allows you to build a structure of components that are independent from your apps.\n\n```python\nCOMPONENTS = {\n    \"libraries\": [\n        \"mysite.components.forms\",\n        \"mysite.components.buttons\",\n        \"mysite.components.cards\",\n    ],\n}\n```\n\n### Disable autodiscovery\n\nIf you specify all the component locations with the setting above and have a lot of apps, you can (very) slightly speed things up by disabling autodiscovery.\n\n```python\nCOMPONENTS = {\n    \"autodiscover\": False,\n}\n```\n\n### Tune the template cache\n\nEach time a template is rendered it is cached to a global in-memory cache (using Python's lru_cache decorator). This speeds up the next render of the component. As the same component is often used many times on the same page, these savings add up. By default the cache holds 128 component templates in memory, which should be enough for most sites. But if you have a lot of components, or if you are using the `template` method of a component to render lots of dynamic templates, you can increase this number. To remove the cache limit altogether and cache everything, set template_cache_size to `None`.\n\n```python\nCOMPONENTS = {\n    \"template_cache_size\": 256,\n}\n```\n\n### Isolate components' context by default\n\nIf you'd like to prevent components from accessing the outer context by default, you can set the `context_behavior` setting to `isolated`. This is useful if you want to make sure that components don't accidentally access the outer context.\n\n```python\nCOMPONENTS = {\n    \"context_behavior\": \"isolated\",\n}\n```\n\n## Logging and debugging\n\nDjango components supports [logging with Django](https://docs.djangoproject.com/en/5.0/howto/logging/#logging-how-to). This can help with troubleshooting.\n\nTo configure logging for Django components, set the `django_components` logger in `LOGGING` in `settings.py` (below).\n\nAlso see the [`settings.py` file in sampleproject](./sampleproject/sampleproject/settings.py) for a real-life example.\n\n```py\nimport logging\nimport sys\n\nLOGGING = {\n    'version': 1,\n    'disable_existing_loggers': False,\n    \"handlers\": {\n        \"console\": {\n            'class': 'logging.StreamHandler',\n            'stream': sys.stdout,\n        },\n    },\n    \"loggers\": {\n        \"django_components\": {\n            \"level\": logging.DEBUG,\n            \"handlers\": [\"console\"],\n        },\n    },\n}\n```\n\n## Management Command\n\nYou can use the built-in management command `startcomponent` to create a django component. The command accepts the following arguments and options:\n\n- `name`: The name of the component to create. This is a required argument.\n\n- `--path`: The path to the components directory. This is an optional argument. If not provided, the command will use the `BASE_DIR` setting from your Django settings.\n\n- `--js`: The name of the JavaScript file. This is an optional argument. The default value is `script.js`.\n\n- `--css`: The name of the CSS file. This is an optional argument. The default value is `style.css`.\n\n- `--template`: The name of the template file. This is an optional argument. The default value is `template.html`.\n\n- `--force`: This option allows you to overwrite existing files if they exist. This is an optional argument.\n\n- `--verbose`: This option allows the command to print additional information during component creation. This is an optional argument.\n\n- `--dry-run`: This option allows you to simulate component creation without actually creating any files. This is an optional argument. The default value is `False`.\n\n### Management Command Usage\n\nTo use the command, run the following command in your terminal:\n\n```bash\npython manage.py startcomponent <name> --path <path> --js <js_filename> --css <css_filename> --template <template_filename> --force --verbose --dry-run\n```\n\nReplace `<name>`, `<path>`, `<js_filename>`, `<css_filename>`, and `<template_filename>` with your desired values.\n\n### Management Command Examples\n\nHere are some examples of how you can use the command:\n\n### Creating a Component with Default Settings\n\nTo create a component with the default settings, you only need to provide the name of the component:\n\n```bash\npython manage.py startcomponent my_component\n```\n\nThis will create a new component named `my_component` in the `components` directory of your Django project. The JavaScript, CSS, and template files will be named `script.js`, `style.css`, and `template.html`, respectively.\n\n### Creating a Component with Custom Settings\n\nYou can also create a component with custom settings by providing additional arguments:\n\n```bash\npython manage.py startcomponent new_component --path my_components --js my_script.js --css my_style.css --template my_template.html\n```\n\nThis will create a new component named `new_component` in the `my_components` directory. The JavaScript, CSS, and template files will be named `my_script.js`, `my_style.css`, and `my_template.html`, respectively.\n\n### Overwriting an Existing Component\n\nIf you want to overwrite an existing component, you can use the `--force` option:\n\n```bash\npython manage.py startcomponent my_component --force\n```\n\nThis will overwrite the existing `my_component` if it exists.\n\n### Simulating Component Creation\n\nIf you want to simulate the creation of a component without actually creating any files, you can use the `--dry-run` option:\n\n```bash\npython manage.py startcomponent my_component --dry-run\n```\n\nThis will simulate the creation of `my_component` without creating any files.\n\n## Community examples\n\nOne 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.\n\n- [django-htmx-components](https://github.com/iwanalabs/django-htmx-components): A set of components for use with [htmx](https://htmx.org/). Try out the [live demo](https://dhc.iwanalabs.com/).\n\n\n## Running django-components project locally\n\n### Install locally and run the tests\n\nStart by forking the project by clicking the **Fork button** up in the right corner in the GitHub . This makes a copy of the repository in your own name. Now you can clone this repository locally and start adding features:\n\n```sh\ngit clone https://github.com/<your GitHub username>/django-components.git\n```\n\nTo quickly run the tests install the local dependencies by running:\n\n```sh\npip install -r requirements-dev.txt\n```\n\nNow you can run the tests to make sure everything works as expected:\n\n```sh\npytest\n```\n\nThe library is also tested across many versions of Python and Django. To run tests that way:\n\n```sh\npyenv install -s 3.6\npyenv install -s 3.7\npyenv install -s 3.8\npyenv install -s 3.9\npyenv install -s 3.10\npyenv install -s 3.11\npyenv install -s 3.12\npyenv local 3.6 3.7 3.8 3.9 3.10 3.11 3.12\ntox -p\n```\n\n### Developing against live Django app\n\nHow do you check that your changes to django-components project will work in an actual Django project?\n\nUse the [sampleproject](./sampleproject/) demo project to validate the changes:\n\n1. Navigate to [sampleproject](./sampleproject/) directory:\n    ```sh\n    cd sampleproject\n    ```\n\n2. Install dependencies from the [requirements.txt](./sampleproject/requirements.txt) file:\n    ```sh\n    pip install -r requirements.txt\n    ```\n\n3. Link to your local version of django-components:\n    ```sh\n    pip install -e ..\n    ```\n    NOTE: The path (in this case `..`) must point to the directory that has the `setup.py` file.\n\n4. Start Django server\n    ```sh\n    python manage.py runserver\n    ```\n\nOnce the server is up, it should be available at <http://127.0.0.1:8000>.\n\nTo display individual components, add them to the `urls.py`, like in the case of <http://127.0.0.1:8000/greeting>\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A way to create simple reusable template components in Django.",
    "version": "0.61",
    "project_urls": {
        "Homepage": "https://github.com/EmilStenstrom/django-components/"
    },
    "split_keywords": [
        "django",
        " components",
        " css",
        " js",
        " html"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "58472c17307f20b25c1e0a6ee4beedda141184cc33dcb84a56d2f080ba979eaf",
                "md5": "153442b521e71421a28877323d32b779",
                "sha256": "c172ad43eaf305395bb899e8d5925cca48b620aa65962b8035a0d3e30e4b4aa0"
            },
            "downloads": -1,
            "filename": "django_components-0.61-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "153442b521e71421a28877323d32b779",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 35347,
            "upload_time": "2024-03-24T11:12:06",
            "upload_time_iso_8601": "2024-03-24T11:12:06.253101Z",
            "url": "https://files.pythonhosted.org/packages/58/47/2c17307f20b25c1e0a6ee4beedda141184cc33dcb84a56d2f080ba979eaf/django_components-0.61-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f65f1947168d32485f0eb313de3a27fb89f45f61ba5650266310918e4ad4f0b2",
                "md5": "813e2bb85dcd3254a33ab0af0d322bf4",
                "sha256": "c5d5fab7fb7f3011d73a85264834c131c301b9d14c900cba366f8d16b67b519c"
            },
            "downloads": -1,
            "filename": "django_components-0.61.tar.gz",
            "has_sig": false,
            "md5_digest": "813e2bb85dcd3254a33ab0af0d322bf4",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 64004,
            "upload_time": "2024-03-24T11:12:08",
            "upload_time_iso_8601": "2024-03-24T11:12:08.734861Z",
            "url": "https://files.pythonhosted.org/packages/f6/5f/1947168d32485f0eb313de3a27fb89f45f61ba5650266310918e4ad4f0b2/django_components-0.61.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-03-24 11:12:08",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "EmilStenstrom",
    "github_project": "django-components",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "django-components"
}
        
Elapsed time: 0.24511s