Create simple reusable template components in Django. https://django-components.github.io/django-components
Find a file
2019-11-23 18:25:54 +01:00
django_components Registering the same component twice only renders one dependency <link> and <script> tag. 2015-06-19 19:30:20 +02:00
tests Change test setup to make test run on Django 1.7. 2015-06-22 22:37:07 +02:00
.gitignore Add GitHubs default gitignore. 2015-06-11 19:58:44 +02:00
.travis.yml More clearly define support 2015-06-14 21:25:27 -05:00
README.md Improve README by adding detailed instructions. 2015-06-19 20:22:58 +02:00
requirements-dev.txt Add requirements files to make local development easier. 2019-11-23 18:25:54 +01:00
requirements.txt Add requirements files to make local development easier. 2019-11-23 18:25:54 +01:00
setup.py Mark Django 1.7 as supported in setup.py 2015-06-22 13:16:56 -05:00
tox.ini Add Django 1.7 to the build matrix 2015-06-22 13:16:56 -05:00

django-components

A way to create simple reusable template components in Django.

Installation

pip install django-components (NOTE: Does not work yet)

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.

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

/* In a file called 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.

/* In a file called script.js */
(function(){
    $(".calendar-component").click(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.

{# In a file called template.html #}
<div class="calendar-component">Today's date is <span>{{ date }}</span></div>

Finally, we use django-components to tie this together. 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.

from django_components import component

class Calendar(component.Component):
    def context(self, date):
        return {
            "date": date,
        }

    class Media:
        template = "[your app]/components/calendar/calendar.html"
        css = {'all': ('[your app]/components/calendar/calendar.css',)}
        js = ('[your app]/components/calendar/calendar.js',)

component.registry.register(name="calendar", component=Calendar)

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

Use the component in a template

First load the django_components tag library, then use the component_dependencies and component tags to render the component to the page.

{% load django_components %}
<!DOCTYPE html>
<html>
<head>
    <title>My example calendar</title>
    {% component_dependencies %}
</head>
<body>
    {% component name="calendar" date="2015-06-19" %}
</body>
<html>

The output from the above template will be:

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

Running the tests

Install tox:

pip install tox

Then run all the tests over all Python versions:

tox

Or just the particular version you wish to test:

tox -e py34

You can also have it watch the directory for changes and re-run the tests:

tox -e py34 -- -f