How do I include an inline bootstrap calendar selector into the Django admin page?
I was creating a custom form and template from the following: Django admin add custom filter It works with a text field. I would like to add the calendar to this.
- How do I include the JS and CSS files for this specific form?
- How do I pass the value selected to my form?
YearMonthForm
class YearMonthForm(forms.Form):
def __init__(self, *args, **kwargs):
self.field_name = kwargs.pop('field_name')
super(YearMonthForm, self).__init__(*args, **kwargs)
self.fields['%s__gte' % self.field_name] = forms.DateField()
YearMonthFilter
class YearMonthFilter(admin.filters.FieldListFilter):
template = 'maindashboard/filter.html'
def __init__(self, *args, **kwargs):
field_path = kwargs['field_path']
self.lookup_kwarg_since = '%s__gte' % field_path
super(YearMonthFilter, self).__init__(*args, **kwargs)
self.form = YearMonthForm(data=self.used_parameters, field_name=field_path)
def expected_parameters(self):
return [self.lookup_kwarg_since]
# no predefined choices
def choices(self, cl):
return []
def queryset(self, request, queryset):
if self.form.is_valid():
filter_params = {
p: self.form.cleaned_data.get(p) for p in self.expected_parameters()
if self.form.cleaned_data.get(p) is not None
}
startBillingDate = filter_params["billingDate__gte"]
year = startBillingDate.year
month = startBillingDate.month
day = calendar.monthrange(startBillingDate.year, startBillingDate.month)[1]
endDate = date(year, month, day)
filter_params["billingDate__lte"] = endDate
return queryset.filter(**filter_params)
else:
return queryset
filter.html
{% load i18n admin_static %}
<h3>{% blocktrans with filter_title=title %} By {{ filter_title }} {% endblocktrans %}</h3>
{% with choices.0 as i %}
<label>Filter by Year-Month:</label>
<div id='InlineMenu'></div>
<form method="GET" action="">
{{ spec.form.media }}
{{ spec.form.as_p }}
<p class="submit-row">
{# create hidden inputs to preserve values from other filters and search field#}
{% for k, v in i.get_query.items %}
<input type="hidden" name="{{ k }}" value="{{ v }}">
{% endfor %}
<input type="submit" value="{% trans "Search" %}">
<input type="reset" value="{% trans "Clear" %}">
</p>
</form>
{% endwith %}
I'm not sure what the {{ spec.form.media }} tags do. But I've seen in other examples where they add a media function to the form.
Folder Structure Folder Structure:
-projectportal
- maindashboard
- templates
- filter.html
- static
- maindashboard
- css
- MonthPicker.min.css
- js
- monthPicker.js
- monthPicker.min.js
How do I make references to the css and js files for this specific form?
Update
Figured out how to reference the JS and CSS files for this form/template. However, the entire Administration page is styled differently now. The calendar is now showing up under the admin filters. Now I need to figure out how to catch the date change events and submit the form.
@property
def media(self):
return forms.Media(
css= {'all': ('maindashboard/css/bootstrap.min.css', 'maindashboard/css/bootstrap-theme.min.css', 'maindashboard/css/bootstrap-datepicker.min.css',)},
js = ('maindashboard/js/jquery-3.3.1.slim.min.js', 'maindashboard/js/bootstrap.min.js', 'maindashboard/js/bootstrap-datepicker.js', 'maindashboard/js/bootstrap-datepicker.min.js', 'maindashboard/js/monthPicker.js'))