Skip to main content

Custom widgets for Django templates

Project description

Widgets for django

Introduction

Widget is template linked with styles and scripts. Application gives you the ability collect media from different widgets and places it in the template wherever you want.

INSTALLATION

You can get Django Widgets Engine by using pip::

$ pip install django-widgets-engine

Initialization

Add to your Django settings

INSTALLED_APPS = [
    #...
    'django_widgets',
    #...
]

MIDDLEWARE = [
    #...
    'django_widgets.middleware.WidgetsMiddleware',
    #...
]

You can also build tag set into templates

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.jinja2.Jinja2',
        'DIRS': [os.path.join(BASE_DIR, 'jinja2')],
        'APP_DIRS': True,
        'OPTIONS': {
            'environment': 'jinja2.Environment',
            'extensions': ['django_widgets.jinja2tags.widget'],
            # ...
        },
    },
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            # ...
            'builtins': [
                'django_widgets.templatetags.widget'
            ],
        },
    },
]

Class Widget

import django_widgets


class WidgetFoo(django_widgets.Widget):
    root = '...'
    # default folder with templates
    template_name = 'template.html'
    # path to template

    class Media:
        extend = False  # include media from parent class
        js = {  # tag <script> for external scripts
            'async': (  # download method (async, defer). Maybe empty string
                'https://absolute/path/to/script.js', # value of attribute src
            ) # tuple
        }
        script = {  # tag <script> for inline scripts
            'text/javascript': {  # value of attribute type
                'script_id': (  # value of attribute id
                    '/path/to/script_template.html',
                )  # tuple
            }
        }
        css = {  # tag <link>
            'all': (  # value of attribute media
                'http://abolute/path/to/style.css', # value of attribute href
            ) # tuple
        }
        style = {  # tag <style>
            'all': (  # value of attribute media
                '/path/to/style_template.css',
            )  # tuple                
        }   

Mixin

When you need use one widget inside other you can use MultiWidgetMixin.

import django_widgets


class MultiWidget(django_widgets.MultiWidgetMixin, django_widgets.Widget):
    template_name = 'multiwidget.html'

    def _init_request(self, request):
        request = super()._init_request(request)
        self.add_widget(request, 'widget_name', WidgetFoo())
        return request

Using

You must use add_widget before using tag widget in template

from django_widgets.api import add_widget
from django.views.generic import TemplateView
from .widgets import WidgetFoo


class MyView(TemplateView):
    template_name = "my_view.html"

    def setup(self, request, *args, **kwargs):
        super().setup(request, *args, **kwargs)
        add_widget(request, 'widget_name', WidgetFoo()) 

Tags

In Django Template before use you need load tag set

{% load widget %}

widget

You can call a widget in a template with data and attributes

{% widget 'widget_name' data=widget_data attrs=widget_attr_dict attr-name=attr_value %}

widgetattrs

Use this tag in widget template. It mixes attributes from template, call widget and widget declaration

{% widgetattrs %}
{% widgetattrs attrs=widget_attr_dict %}
{% widgetattrs attr-name=attr_value %}

widgetmedia

{% widgetmedia %}  {# output all media for all widgets #}
{% widgetmedia 'js' %} {# print <script src="...">  for all widgets #}
{% widgetmedia 'css' %} {# print <link> for all widgets #}
{% widgetmedia 'style' %} {# print <style> for all widgets #}
{% widgetmedia 'script' name='widget_name'  %} {# print <script> for widjet with specified name #}
{% widgetmedia name='widget_name' %}  {# output all media for widjet with specified name#}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-widgets-engine-1.0.1.tar.gz (26.3 kB view details)

Uploaded Source

File details

Details for the file django-widgets-engine-1.0.1.tar.gz.

File metadata

  • Download URL: django-widgets-engine-1.0.1.tar.gz
  • Upload date:
  • Size: 26.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.2

File hashes

Hashes for django-widgets-engine-1.0.1.tar.gz
Algorithm Hash digest
SHA256 a38a64db1003aa51f91d50cf7ddf34927b7bcddc68464a90d7fd20deab179e17
MD5 a221f11dce78fcf79773e2977e69f93b
BLAKE2b-256 8d9b091fec6e681d09e939c4de6db429255be320b867c1f0acdf390cd9e3a12b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page