link to individually prefiltered data (in template)

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 :slight_smile:

You can supply a variable through your context to the rendering engine when you’re creating your URL.

For example, you could do something like:
<a href="{% url 'plants:plants' %}?operating_environment={{ plant.operating_environment}}">
(Other parameters removed for clarity)

1 Like

I am not sure if I understand the question directly. So apologizes if not relevant.

I think what you want is not a href tag, but a form with inputs (could be hidden inputs) for your filtering parameters. When the user clicks the “submit” button, you post against a view and return the same template with a different context. Here is a simplified example.

#models.py
from django.db import models

class Person(models.Model):
   class Gender(models.TextChoices):
         Male = 'm', "Male"
         Female = "f", "Female"
         Other = "o", "Other"
   name= models.CharField(max_length=250)
   title= models.TextField()
   gender = models.CharField (max_length=1, choices= Gender.choices)

#views.py
def people(request):
     people = Person.objects.all()
      if request.method == "POST":
           gender = request.POST['gender']
           people = Person.objects.filter(gender=gender)
     return render (request, "people.html", "people": people)

def one_person (request, person_id):
     person= get_object_or_404(Person, pk=person_id)
     return render (request, "person.html", "person": person)

# people.html
<h3>People</h3>
{% for person in people %}
<p><b>Name: </b>{{person.name}}</p>
<p><b>Title: </b>{{person.title}}</p>
<p><b>Gender: </b> 
<form method="post" action= "{% url 'people' %}">
{% csrf_token %}
<input type="text" readonly value="{{person.gender}} name="gender" />
<input type="submit" value="Filter">
 </form>
</p>
<a href= {% url 'person' person.pk' %}> Details </a>
{% endfor %}  

Hope that makes sense!