[Django]-.js files not included in page when using Django Form Assets (Media class) with crispy forms

1👍

I had the same problem just now…and have 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 the form.media to your output, but that assumes your form is called 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())))

You could probably also call insert() instead of append.

Leave a comment