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

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

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

    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

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 :

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 :

 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 .

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.

1 Like

KenWhitesell can you please show me or any documentation how to add two different parameter for url in same views?

It’s covered in the docs.

1 Like

KenWhitesell Thanks. My problem solved