'Django: Crispy forms on django-filters removes selected on drop-down lists
Why does | crispy remove the selected attribute on drop-down lists of my filters? It does not show the currently applied filter.
Given the following forms.py:
class TaskListView(LoginRequiredMixin, ListView):
model = Task
paginate_by = 10
def get_queryset(self):
# all groups for user
groups = self.request.user.groups.values_list("pk", flat=True)
groups_as_list = list(groups)
queryset = (
Task.objects.filter(object__owner=self.request.user)
)
return queryset
def get(self, request, *args, **kwargs):
filterset = TaskFilter(request.GET, queryset=self.queryset)
self.object_list = filterset.qs
context = self.get_context_data(filter=filterset, object_list=self.object_list)
return self.render_to_response(context)
And filter.py:
from .models import Task
import django_filters
class TaskFilter(django_filters.FilterSet):
class Meta:
model = Task
fields = ["status"]
I'm calling the following URL: http://localhost:8000/task/?status=30
With {{ filter.form }} the template generates the following output:
<select name="status" id="id_status">
<option value="" >---------</option>
<option value="10" >Overdue</option>
<option value="20" >Due</option>
<option value="30" selected>Pending</option>
<option value="40" >Inactive</option>
</select>
...
With {{ filter.form | crispy }} the template generates the following output, which is missing the selected value:
<select class="bg-white ..." name="status" >
<option value="" >---------</option>
<option value="10" >Overdue</option>
<option value="20" >Due</option>
<option value="30" >Pending</option>
<option value="40" >Inactive</option>
</select>
Any idea why the selected is no longer in place and the id="id_status" is gone?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
