Skip to main content

JavaScript utilities for Django projects.

Project description

django-gesha ☕

Test Version Python Django

JavaScript utilities for Django projects.

Current features:

  • Easily add JavaScript context in class-based views and access it via a JavaScript API.

Many more features are planned ✍️.

Installation

Install to Python environment using pip

pip install django-gesha

Install app in Django project

Add gesha to INSTALLED_APPS in your Django project's settings:

INSTALLED_APPS = [
    ...
    "gesha",
]

Collect JavaScript assets

Run the collectstatic management command to collect django-gesha's JavaScript files. Django should locate them automatically once the app is installed.

python manage.py collectstatic

Basic usage

Add mixin to class-based view

For example:

from django.views.generic import TemplateView
from gesha.mixins import JSContextMixin


class MyPage(JSContextMixin, TemplateView):
    template_name = "myapp/mypage.html"

    def get_js_context_data(self, **kwargs) -> typing.Dict:
        context = super().get_js_context_data(**kwargs)
        context.update({
            "myNumber": 5,
            "myString": "this is my string",
        })
        return context

Load django-gesha JavaScript assets in template

To use django-gesha's JavaScript API, load the script in your template, and load the context using the jscontext template tag:

{% load gesha static %}

{% jscontext %}

<script src="{% static 'gesha/dist/js/django-gesha.bundle.min.js' %}">
</script>

Test in JavaScript

Access the context data using the django JavaScript API:

>> console.log(django.context.myNumber)
   5

>> console.log(django.context.myString)
   "this is my string"

Contributing

See Contributing.

License

MIT.

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-gesha-0.1a0.tar.gz (99.5 kB view hashes)

Uploaded Source

Built Distribution

django_gesha-0.1a0-py3-none-any.whl (6.6 kB view hashes)

Uploaded Python 3

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