# View how to control what is in queryset from html control

**URL:** <https://forum.djangoproject.com/t/view-how-to-control-what-is-in-queryset-from-html-control/12847>\
**Category:** Forms & APIs\
**Created:** [March 27, 2022, 6:34pm UTC](https://forum.djangoproject.com/t/view-how-to-control-what-is-in-queryset-from-html-control/12847 "2022-03-27T18:34:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![GarikFirst](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/garikfirst/32/7910_2.png) [@GarikFirst](https://forum.djangoproject.com/u/GarikFirst)\
**Post date:** [March 27, 2022, 6:34pm UTC](https://forum.djangoproject.com/t/view-how-to-control-what-is-in-queryset-from-html-control/12847/1 "2022-03-27T18:34:37Z")

</div>

Hello!

I want somehow filter with control on html page what is in queryset for my view

**My model**

```auto
class Production(models.Model):
    '''Выработка.'''
    project = models.ForeignKey(
        Project, on_delete=models.PROTECT, verbose_name='проект')
    timeperiod = models.ForeignKey(
        Timeperiod, on_delete=models.PROTECT, verbose_name='период')
    time = models.DurationField('трудочасы')
    ammount = models.DecimalField('выработка', max_digits=10, decimal_places=2)
    is_auto = models.BooleanField(
        'признак автоматического расчета', default=False)

```

**My view**

```auto
class Production(LoginRequiredMixin, ListView):
    login_url = reverse_lazy('collector:login')
    queryset = Production.objects.filter(project __title__ contains=‘MYPARAM')
    context_object_name = 'productions'
    template_name = 'collector/production.html'

```

What i would like to achieve is on my attached picture

 ![2022-03-27 21.31.45](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/0/006993f2b518d4995d4594a92dbd2ee8447f9df2.jpeg)

But I’m struggling

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 27, 2022, 6:40pm UTC](https://forum.djangoproject.com/t/view-how-to-control-what-is-in-queryset-from-html-control/12847/2 "2022-03-27T18:40:10Z")

</div>

Do you want that select field to call your view with that value to pass as a parameter? You can do that in JavaScript, catching the on\_change event for that select box.

If you want that selector to change what’s currently displayed _without_ fetching data from the server, then that _all_ needs to be done within JavaScript. (You’ll also be catching the on\_change event, but now it becomes your responsibility to write the JavaScript code to show or hide the data to be displayed.)

---

<div class="post-metadata">

**Author:** ![GarikFirst](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/garikfirst/32/7910_2.png) [@GarikFirst](https://forum.djangoproject.com/u/GarikFirst)\
**Post date:** [March 28, 2022, 6:15am UTC](https://forum.djangoproject.com/t/view-how-to-control-what-is-in-queryset-from-html-control/12847/3 "2022-03-28T06:15:56Z")

</div>

Thank you for your answer!

---

<div class="post-metadata">

**Author:** ![GarikFirst](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/garikfirst/32/7910_2.png) [@GarikFirst](https://forum.djangoproject.com/u/GarikFirst)\
**Post date:** [March 28, 2022, 4:18pm UTC](https://forum.djangoproject.com/t/view-how-to-control-what-is-in-queryset-from-html-control/12847/4 "2022-03-28T16:18:39Z")

</div>

For the history - I was able to achieve this without JS at all (but in such clean way as I desire - there is refresh for all page). Please, criticise this approach.

Here is example of concept:

**Model**

```auto
class TestModel(models.Model):
    title = models.CharField(’title', max_length=50)
    number = models.PositiveIntegerField(’number')

```

**View**

```auto
class TestView(ListView):
    template_name = 'collector/test.html'
    model = TestModel
    context_object_name = 'data'

    def get_queryset(self) -> QuerySet:
        title_id = self.request.GET.get('title')
        return TestModel.objects.filter(id=title_id)

    def get_context_data(self, **kwargs) -> Dict:
        context = super().get_context_data(**kwargs)
        context['titles'] = TestModel.objects.all()
        return context

```

**Template**

```auto
<form method="get">
  <select class="form-select" name="title">
    {% for title in titles %}
    <option value = "{{ title.id }}" {% if request.GET.title|add:"0" == title.id|add:"0" %}selected{% endif %}>{{ title.title }}</option>
    {% endfor %}
  </select>
  <button type="submit" class="btn">Фильтровать</button>
<form>
<table class="table">
  <thead>
    <tr>
      <th>id</th>
      <th>number</th>
    </tr>
  </thead>
  <tbody>
    {% for d in data %}
    <tr>
      <td>{{ d.title}} </td>
      <td>{{ d.number }}</td>
    </tr>
    {% endfor %}
  </tbody>
</table>

```

---

<div class="post-metadata">

**Author:** ![dcdinh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/dcdinh/32/8110_2.png) [@dcdinh](https://forum.djangoproject.com/u/dcdinh)\
**Post date:** [April 13, 2022, 4:50am UTC](https://forum.djangoproject.com/t/view-how-to-control-what-is-in-queryset-from-html-control/12847/5 "2022-04-13T04:50:17Z")

</div>

**model**

```auto
class Company(models.Model):
    code = models.CharField(max_length=25, unique=True)
    name = models.CharField(max_length=255, null=True, blank=True)
    
class Service(models.Model):
    code = models.CharField(max_length=25,unique=True, null=True, blank=True)
    name = models.CharField(max_length=255)
    unit_price = models.IntegerField()

    def __str__ (self):
        return self.name
class ServiceCompany(models.Model):
    company = models.ForeignKey(Company, on_delete=models.SET_NULL, null=True)
    service = models.ForeignKey(Service, on_delete=models.SET_NULL, null=True)
    qty = models.IntegerField()
    
    def __str__ (self):
        return self.company.name

```

**view**

```auto
@login_required
def report(request):
    services = Service.objects.all()
    companies = Company.objects.all()
    code = request.GET.get('code')
    company = Company.objects.get(code=code)
    services_used = ServiceCompany.objects.filter(company=company)
    return render(request, "export_data.html", {"services": services, 'company_list': companies, 'code': code, "services_used":(services_used)})

```

**template**

```auto
 <script type="text/javascript">
    const services = "{{services_used}}";
    
    console.log(services);
</script>

```

How do i set value of queryset from views.py to variable in javascript?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 13, 2022, 12:04pm UTC](https://forum.djangoproject.com/t/view-how-to-control-what-is-in-queryset-from-html-control/12847/6 "2022-04-13T12:04:23Z")

</div>

See the [json\_script](https://docs.djangoproject.com/en/4.0/ref/templates/builtins/#json-script) tag.

---

<div class="post-metadata">

**Author:** ![dcdinh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/dcdinh/32/8110_2.png) [@dcdinh](https://forum.djangoproject.com/u/dcdinh)\
**Post date:** [April 14, 2022, 1:46am UTC](https://forum.djangoproject.com/t/view-how-to-control-what-is-in-queryset-from-html-control/12847/7 "2022-04-14T01:46:49Z")

</div>

i found answer which i need in Django docs. Thank you very much!
