I wrote an function for download csv file. I have two type of user doctor and patient. When any user visit patient.html page he can download all patient data. I know I can write another function for downloads all doctor data when anyone visit doctor.html page but I want to do it in same function. here is my code:
def exportcsv(request,slug=None):
response = HttpResponse(content_type='text/csv')
writer = csv.writer(response)
all_patient = Patient.objects.all()
all_doctor = Doctor.objects.all()
if slug == None:
if all_patient:
response ['Content-Disposition'] = 'attachment; filename=AllPatientData'+str(datetime.date.today())+'.csv'
writer.writerow(['patient_id','patient_name','date_of_birth','age','phone','email','gender','country','state','city','zip_code','address'])
for i in all_patient:
writer.writerow([i.patient_id,i.patient_name,i.date_of_birth,i.age,i.phone,i.email,i.gender,i.country,i.state,i.city,i.zip_code,i.address])
if all_doctor:
response ['Content-Disposition'] = 'attachment; filename=AllDoctorData'+str(datetime.date.today())+'.csv'
writer.writerow(['docter_id_num','doctor_name','doctor_speciality','doctor_experience','date_joined'])
for i in all_doctor:
writer.writerow([i.docter_id_num,i.doctor_name,i.doctor_speciality,i.doctor_experience,i.date_joined])
return response
my urls.py
path('export-csv-all-data/',views.exportcsv,name='export-csv-all-data'),
now itâs downloading all patients and doctor data together. How to create another ulr parameter for downloading all doctor ?