.js files not included in the page when using Django Form properties (media class) with crispy forms

I reproduce the main example in the Django documentation ( doc ) on how to automatically include .js or .css files from a custom calendar widget:

class CalendarWidget(forms.TextInput):
class Media:

    js = ('test.js',)

When using this CalenderWidget in a simple form field definition, the test.js file is not entered on the page. I use crispy shapes to display the shape in my template, so I assume this should be the source of the problem ...

Currently, I have to add the {{form.media}} tag to my template to force the test.js file to be added to the page, but I would like to find a way to make things more “automatic” as if it should be ...

thanks a lot

+4
source share
1 answer

I had the same problem just now ... and found a workaround or two.

Start with:

class MyForm(Form):
    def __init__(self, *args, **kwargs):
        super(MyForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper(self)

Then:

1) You can add form.media to your output, but that assumes your form is called a form in your context.

        self.helper.layout.append(Layout(HTML("{{ form.media }}")))

2) You can call self.media.render () - which I think is better:

        self.helper.layout.append(Layout(HTML(self.media.render())))

Perhaps you can also call insert () instead of append.

+1
source

All Articles