Usage

Registration of resources

In order to use django-resources you first have to register some CSS or Javascript files. You can do this via the provided utility methods:

from resources.utils import register_resource
from resources.config import CSS
from resources.config import JS

register_resource("/path/to/css_sheet_1.css", type=CSS, merge=True, minify=True, group=None, media="screen", position=10)
register_resource("/path/to/css_sheet_2.css", type=CSS, merge=True, minify=True, group=None, media="screen, print", position=20)

register_resource("/path/to/javascript_1.js", type=JS, merge=True, minify=True, group=None, position=20)

Which means:

  1. We register two CSS sheets which should be merged and minified whithin the global CSS sheet (no group). resource_1 should be before resource_2
  2. We register a Javascript script which should be merged and minified within the global Javscript file (no group)

Creating merged resources

In order to create merged and minfied resources out of registered ones, just call:

http://localhost:8000/create-resources

Using in HTML

In order to use the registered resources whithin your HTML use the provided tags:

<html>
    <head>
        {% css %}
        {% javascript %}
    <head>
</head>

This will render the merged and minified global css and javascript sheets.

Using groups

With groups you can group specific resources together. Alle resources which have the same group and registered with merged = True are merged together:

from resources.utils import register_resource
from resources.config import CSS

register_resource("/path/to/css_sheet_1.css", type=CSS, merge=True, minify=True, group="main", position=10)
register_resource("/path/to/css_sheet_2.css", type=CSS, merge=True, minify=True, group="main", position=20)
register_resource("/path/to/css_sheet_3.css", type=CSS, merge=True, minify=True, group="manage", position=10)
register_resource("/path/to/css_sheet_4.css", type=CSS, merge=True, minify=True, group="manage", position=20)

That means:

  1. css_sheet_1 and css_sheet_2 are merged together to the file main_xyz.css
  2. css_sheet_3 and css_sheet_4 are merged in another file manage_xyz.css

Using groups in HTML

In order to use the registered resources for a specific group whithin your HTML use the provided tags:

<html>
    <head>
        {% css 'main' %}
    <head>
</head>

This will render the merged and minified css sheets for the group main.

Project Versions

Table Of Contents

Previous topic

Installation

Next topic

API

This Page