Reputation: 1731
I added a custom page containing a form in my Django admin site. To do this, I wrote this code:
class MyForm(forms.Form):
class Media:
js = ('js/foo.js',)
my_number = forms.IntegerField(
required=True,
min_value=1,
max_value=10,
label='Magic number'
)
And this code:
form = MyForm()
context = self.each_context(request)
context.update({'form': form})
return render(request, 'admin/players/foo.html', context)
My problem is the JS does not execute/load because if I put the following single line in it, nothing happens:
alert('foo');
What's strange is I have a very similar thing somewhere else and it works like a charm:
class MyModelAdmin(admin.ModelAdmin):
list_display = ('bar1', 'bar2',)
search_fields = ('bar1',)
class Media:
js = ('js/bar.js',)
I checked the media
property in both cases and I got this:
<script type="text/javascript" src="/static/js/foo.js"></script>
<script type="text/javascript" src="/static/js/bar.js"></script>
So at least, the path to the file is OK.
What did I miss?
UPDATE: Following the accepted answer, I added the following lines in my template:
{% block extrahead %}
{{ form.media }}
{% endblock %}
Upvotes: 1
Views: 1683