Djano how to create two different url parameter from same view for run two different logic?

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('.csv'
             for i in all_patient:
        if all_doctor:
             response ['Content-Disposition'] = 'attachment; filename=AllDoctorData'+str('.csv'
             for i in all_doctor:

    return response



now it’s downloading all patients and doctor data together. How to create another ulr parameter for downloading all doctor ?

Start by reviewing the documentation at URL dispatcher | Django documentation | Django.

If you’ve got a particular question about some aspect of url handling, it would help if you would be more specific with what you’re asking.

KenWhitesell I read it. My problems is little bit different. Document showing how to write different url parameter for single objects. such as I have few blog so I can write slug url parameter for view every blog. They are appropriate for details page where we can view only single objects at a time. I want to do it for my list page. Now I am downloading all patient and doctor data from this url path('export-csv-all-data/',views.exportcsv,name='export-csv-all-data'),. Now I can write different url parameter for my exportcsv view for downlaoding all doctor data

The type and nature of the url parameters are independent of how they’re used. You’re creating a connection or correlation in your mind that doesn’t exist within the system.

A parameter in the URL is passed as a parameter to the view. What the view does with those parameters is a completely separate and unrelated issue.

1 Like

KenWhitesell let ask you few question.

assume I add an url somethings like this path('export-csv-all-doctor-data/<str:is_doctor>',views.exportcsv,name=export-csv-all-doctor-data'),
question 1
is it mandatory to add is_doctor fields in my doctor model ?

if it’s not mandatory then how to write this url in html template. More specifically how to write it in list view page? some things like this {%url 'export-csv-all-doctor-data' %} or do I need to write like slug url {%url 'export-csv-all-doctor-data' str=is_doctor %}

if I don’t need to add any fields in my models then how to add this url parameter in my function based view? something like this

def exportcsv(request,is_doctor=None):


First, str is the data type, not the variable name. You wouldn’t use str= there, it would be is_doctor=, since that’s the parameter name in your url and view definition. You could write: {% url 'export-csv-all-doctor-data' is_doctor=is_doctor %} or just {% url 'export-csv-all-doctor-data' is_doctor %}

But what you specify there is going to depend upon what the value of “is_doctor” is going to be. If you’re looking to use the literal string ‘is_doctor’, then it would be 'is_doctor' and not is_doctor. You would use the latter if is_doctor is a variable in your context being rendered by the template. (You haven’t shown what you’re going do with that parameter, so I can’t properly answer your question.)

That’s correct.

1 Like


As you said is_doctor= , will be the parameter name in my url and view definition. In my views I used this:

def exportcsv(request,slug=None,is_doctor=None): others code. then I added this logic
    if is_doctor != None: others code

in my I added this

getting this error

raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: URL route 'export-csv-all-doctor-data/<is_doctor:is_doctor>/' uses invalid converter 'is_doctor'.

error saying uses invalid converter 'is_doctor'. what does mean? where I am doing mistake ?

Review the examples at: URL dispatcher | Django documentation | Django


is the correct URL definition. You’re specifying that the URL will have a parameter named is_doctor, of data type str.

1 Like

KenWhitesell The previous error gone. Now I am getting this error when visiting the list view page

Reverse for 'export-csv-all-doctor-data' with arguments '('',)' not found. 1 pattern(s) tried: ['export\\-csv\\-all\\-doctor\\-data/(?P<is_doctor>[^/]+)$']

now my


I think I am doing mistake writing url in my template

{% url 'hospital:export-csv-all-doctor-data' is_doctor %}

I also tried this

{% url 'hospital:export-csv-all-doctor-data' is_doctor=is_doctor %}

Answered above:

Is is_doctor in your template supposed to be a literal string or a variable passed to the template in the context?

1 Like

KenWhitesell Thanks for details explanation. Now my problem solved.