2

I have successfully used django-widget-tweaks add_class as Jinja filter. I put the function into Jinja environment filters, like below.

def environment(**options):
    env = Environment(**options)
    env.globals.update({
        'static': staticfiles_storage.url,
        'url': reverse,
        'render_field': widget_tweaks.render_field,
    })
    env.filters.update({
        'add_class': widget_tweaks.add_class,
        'set_attr': widget_tweaks.set_attr,
    })
    return env

Now I have new requirement to add attribute "value" of an input field form with model type DateField, I need to pass value of the current date via context variable to Jinja template.

I have tried to find how to pass variable to attribute, many SO article mention about using render_field [1].

I can't use render_field as Jinjaa global function (like snippet above). I need code similar to this:

{{ render_field(form.user_date, value={{ today }}) }}

Is there anyone success using render_field as Jinja function?

I found someone using render_field as Jinja global function [2], but its not using the existing render_field, they create new function. But I still not success with this function.

appreciate for any help.

[1]https://github.com/jazzband/django-widget-tweaks

[2]https://github.com/niwinz/django-jinja/blob/e38fef68ce363d49727429c7b34ea44f45553f5d/django_jinja/contrib/_widget_tweaks/templatetags/_render_field.py

oon arfiandwi
  • 515
  • 1
  • 8
  • 21

0 Answers0