Skip to main content

django-js-choices

Packaging: poetry Code style: black Build status Code coverage PyPi version Downloads

Overview

Django JS Choices makes handling of model field choices in javascript easy.

For example, given the model…

# models.py:

class Student(models.Model):
    FRESHMAN = 'FR'
    SOPHOMORE = 'SO'
    JUNIOR = 'JR'
    SENIOR = 'SR'
    YEAR_IN_SCHOOL_CHOICES = (
        (FRESHMAN, 'Freshman'),
        (SOPHOMORE, 'Sophomore'),
        (JUNIOR, 'Junior'),
        (SENIOR, 'Senior'),
    )
    year_in_school = models.CharField(
        max_length=2,
        choices=YEAR_IN_SCHOOL_CHOICES,
        default=FRESHMAN,
    )

…the choices are accesible in javascript.

Choices.pairs("year_in_school");

Result:

[
    {value: "FR", label: "Freshman"},
    {value: "SO", label: "Sophomore"},
    {value: "JR", label: "Junior"},
    {value: "SR", label: "Senior"}
]

Display values are also accesible.

Choices.display("year_in_school", "FR")
Choices.display("year_in_school", {"year_in_school": "FR"})

In both cases the result is

"Freshman"

Installation

Install using pip…

pip install django-js-choices

…or clone the project from GitHub.

git clone https://github.com/lorinkoz/django-js-choices.git

Add 'django_js_choices' to your INSTALLED_APPS setting.

INSTALLED_APPS = (
    ...
    'django_js_choices',
)

Usage as static file

First generate static file by

python manage.py collectstatic_js_choices

If you add apps, models, or change some existing choices, you may update the choices.js file by running the command again.

The choices files is created with the locale prefix defined in your settings, but you can pass any locale to the command…

python manage.py collectstatic_js_choices --locale es

In this case, the generated file will be choices-es.js.

After this add the file to your template.

<script src="{% static 'choices-es.js' %}"></script>

Usage with views

Include non-cached view…

from django_js_choices.views import choices_js

urlpatterns = [
    path("jschoices/", choices_js, name="js_choices"),
]

…or use cache to save some bandwith.

from django_js_choices.views import choices_js

urlpatterns = [
    path("jschoices/", cache_page(3600)(choices_js), name="js_choices"),
]

Include javascript in your template.

<script src="{% url 'js_choices' %}" type="text/javascript"></script>

Usage as template tag

If you want to generate the javascript code inline, use the template tag.

{% load js_choices %}
<script type="text/javascript" charset="utf-8">
    {% js_choices_inline %}
</script>

Use the choices in javascript

For every model field with choices, they will be available by the following names.

Choices.pairs("<app_label>_<model_name>_<field_name>")
Choices.pairs("<model_name>_<field_name>")
Choices.pairs("<field_name>")

If any of these names conflict with other model fields, the conflicting names won’t be accessible to prevent ambiguity.

Register other choices not in models

If you have some choices you want to expose to javascript, and they don’t fit into any model, lets say for example, a list of actions, you can add those too:

from django_js_choices.core import register_choice

POSSIBLE_ACTIONS = (
    ("go_down", "Go down"),
    ("go_top", "Go top"),
    ("nothing", "Stay")
)
...
# register_choice(name: string, choices: list)
register_choice("possible_actions", POSSIBLE_ACTIONS)
  • If any of the names of a manually registered choice conflicts with some model fields, the one you manually registered will be the one you’ll access.

  • The 2nd argument is the same type that you’d pass to a CharField choices argument.

You can only access the POSSIBLE_ACTIONS choices through the name possible_actions

PLEASE NOTE: You must ensure the file where you are registering your choice is been processed by django

Choices.pairs("possible_actions")

Options

Optionally, you can overwrite the default javascript variable ‘Choices’ used to access the choices by Django setting.

JS_CHOICES_JS_VAR_NAME = 'Choices'

Optionally, you can change the name of the global object the javascript variable used to access the choices is attached to. Default is this.

JS_CHOICES_JS_GLOBAL_OBJECT_NAME = 'window'

Optionally, you can disable the minfication of the generated javascript file by Django setting.

JS_CHOICES_JS_MINIFY = False

Contributing

  • PRs are welcome!

  • To run the test suite run make or make coverage. The tests for this project live inside a small django project called sandbox.

Credits

Inspired by (and conceptually forked from) django-js-reverse

Metadata

Release files for django-js-choices 0.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-js-choices 0.6.0
File Size Uploaded Provenance Actions
django_js_choices-0.6.0.tar.gz 8.4 kB Without provenance Details for django_js_choices-0.6.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-js-choices 0.6.0
File Interpreter ABI Platform Actions
django_js_choices-0.6.0-py3-none-any.whl Python 3 none any Details for django_js_choices-0.6.0-py3-none-any.whl

Total release size: 17.1 kB

Release files / django_js_choices-0.6.0.tar.gz

Download URL django_js_choices-0.6.0.tar.gz
Size 8.4 kB
Tags Source
SHA-256 checksum
How to use checksums
dbb1c452e43ab1cd8dbac1a7b9f7a5456e59bc6a12c0b36feb66d98425d1c4f8
BLAKE2b-256 checksum
How to use checksums
46b660570fe22a33b4fae85d0486e627a6b504e31114f0ac4d63f0ff926f42f3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

Release files / django_js_choices-0.6.0-py3-none-any.whl

Download URL django_js_choices-0.6.0-py3-none-any.whl
Size 8.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b18f6ca4a4bff8bfa572b0cad5ef3e48603bbf1794913a6a2914ee96cd618fc4
BLAKE2b-256 checksum
How to use checksums
c81436b0e8fc2d3fd7f3d3add24bad7cc241461c583f151edd13e045549b1be1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

Security for django-js-choices 0.6.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page