# Django how to pass two different query in same view for export csv file from two different html page?

**URL:** <https://forum.djangoproject.com/t/django-how-to-pass-two-different-query-in-same-view-for-export-csv-file-from-two-different-html-page/11341>\
**Category:** Using Django\
**Created:** [January 3, 2022, 10:09am UTC](https://forum.djangoproject.com/t/django-how-to-pass-two-different-query-in-same-view-for-export-csv-file-from-two-different-html-page/11341 "2022-01-03T10:09:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![MDFARHYN](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mdfarhyn/32/4519_2.png) [@MDFARHYN](https://forum.djangoproject.com/u/MDFARHYN)\
**Post date:** [January 3, 2022, 10:09am UTC](https://forum.djangoproject.com/t/django-how-to-pass-two-different-query-in-same-view-for-export-csv-file-from-two-different-html-page/11341/1 "2022-01-03T10:09:06Z")

</div>

I am using this query `patient = Patient.objects.all()` for exporting all objects in csv file and this query `Patient.objects.filter(slug=slug)` for exporting single objects in csv. The main problem it’s downloading single objects and all objects together in a csv file. I want single object query only run when I will be on details page and all objects query will be run when I will be on list page. here is my code:

here is my code:

**models.py**

```auto
class Patient(models.Model):
      patient_name = models.CharField(max_length=150)
      patient_id = models.CharField(max_length=100,blank=True,null=True)
      date_of_birth = models.DateField()
      age = models.CharField(max_length=100,blank=True,null=True)
      phone = models.CharField(max_length=100)
      email = models.EmailField(blank=True,null=True)
      slug = models.SlugField(max_length=255,unique=True,blank=True,null=True)

```

**views.py**

```auto
    def exportcsv(request): #I am exporting csv file from this view
        response = HttpResponse(content_type='text/csv') 
        response ['Content-Disposition'] = 'attachment; filename=PatientData'+str(datetime.datetime.now())+'.csv'
    
        writer = csv.writer(response)
        writer.writerow(['patient_name'])
        
        all_patient_objects = Patient.objects.all() #exporting all objects
        single_patients_objects = Patient.objects.filter(slug=slug) #exporting single objects 

        for i in single_patients_objects: #want to run this loop only in details page
            print(i.patient_name)
            writer.writerow([i.patient_name])

        for i in all_patient_objects: #want to run this loop only in list page
            print(i.patient_name)
            writer.writerow([i.patient_name])

        return response

```

**#urls.py**

```auto
path('all-patient/',views.AllPatient,name='all-patient'), #list page
path('<slug:slug>/patient-details/',PatientDetails.as_view(),name='patient-details'), #details page
path('<slug:slug>/export-csv-patient-data/',views.exportcsv,name='export-csv-patient-data'),

```

Now it’s downloading all objects and single objects together in a single csv file. I want it will download only single objects from details page and all objects from list page.

I want to run this loop only in **details page** for **exporting single object** :

```auto
for i in single_patients_objects: #want to run this loop only in details page
            print(i.patient_name)
            writer.writerow([i.patient_name])

```

I want to run this loop only in **list page** for **download all objects** :

```auto
 for i in all_patient_objects: #want to run this loop only in list page
            print(i.patient_name)
            writer.writerow([i.patient_name])

```

I want to download two different csv. When I will be on details page then it will download single object from details page and when on list page then I will download all objects from list page. Basically I have two for loop and want to run only one for loop at a time depend on page .

---

<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:** [January 3, 2022, 1:10pm UTC](https://forum.djangoproject.com/t/django-how-to-pass-two-different-query-in-same-view-for-export-csv-file-from-two-different-html-page/11341/2 "2022-01-03T13:10:26Z")

</div>

Add a parameter to the url for export, and have each of the buttons on your details and list page supply a different value for that parameter. Test that parameter in your view to determine what to do.

---

<div class="post-metadata">

**Author:** ![MDFARHYN](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mdfarhyn/32/4519_2.png) [@MDFARHYN](https://forum.djangoproject.com/u/MDFARHYN)\
**Post date:** [January 3, 2022, 1:28pm UTC](https://forum.djangoproject.com/t/django-how-to-pass-two-different-query-in-same-view-for-export-csv-file-from-two-different-html-page/11341/3 "2022-01-03T13:28:20Z")

</div>

[KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell) can you please show me or any documentation how to add two different parameter for url in same views?

---

<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:** [January 3, 2022, 1:34pm UTC](https://forum.djangoproject.com/t/django-how-to-pass-two-different-query-in-same-view-for-export-csv-file-from-two-different-html-page/11341/4 "2022-01-03T13:34:47Z")

</div>

It’s covered in the docs.

- [URL dispatcher | Django documentation | Django](https://docs.djangoproject.com/en/4.0/topics/http/urls/#url-dispatcher)

---

<div class="post-metadata">

**Author:** ![MDFARHYN](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mdfarhyn/32/4519_2.png) [@MDFARHYN](https://forum.djangoproject.com/u/MDFARHYN)\
**Post date:** [January 3, 2022, 1:42pm UTC](https://forum.djangoproject.com/t/django-how-to-pass-two-different-query-in-same-view-for-export-csv-file-from-two-different-html-page/11341/5 "2022-01-03T13:42:09Z")

</div>

KenWhitesell Thanks. My problem solved
