Hej!
I have multiple views, some with result tables and some detail views.
From the detail views there should be links to other detail views (which works) and to some of the result tables with prefiltered results. For example after the name given in the detail view.
If clicked on the button I want to get redirected to the result table already filtered for the operating environment of (e.g.) given in the detail view.
But I want to use the same template for all detail views (every plant gets a detail view) so it should be universal and not hard coded.
Does anyone knows I could achieve that?
# views.py
def plants_view(request):
plants = Plant.objects.all()
myFilter = PlantFilter(request.GET, queryset=plants)
plants = myFilter.qs
context = {"plants": plants, "myFilter": myFilter}
return render(request, 'plants/search_table.html', context)
class PlantDetail(DetailView):
model = Plant
def get(self, request, *args, **kwargs):
plant = get_object_or_404(Plant, pk=kwargs['pk'])
context = {'plant': plant}
return render(request, 'plants/detail.html', context)
# filters.py
class PlantFilter(django_filters.FilterSet):
name = CharFilter(field_name="name", lookup_expr='icontains')
possession_of_plant = ModelChoiceFilter(queryset=PossessionOfPlant.objects.all())
class Meta:
model = Plant
fields = '__all__'
<!-- detail.html -->
<h3>Plant</h3>
<p><b>Name: </b>{{plant.name}}</p>
<p><b>Plant type: </b>{{plant.plant_type}}</p>
<p><b>Operating Environment: </b>
<a href="{% url 'plants:plants' %}?name=&plant_type=&operating_environment=pilot&nuts_level_3=&comment=&possession_of_plant=">
{{plant.operating_environment}}</a></p>
Like that I can pre-filter in the link, but it is hard coded for operating_environment=pilot. What I want is to filter for the exact operating_environment used in the plant/detail view. Is there a way to achieve that?
Any help is appreciated!
If you only know where I should look thats totally fine with me!
Best regards