How To Pass Multiple Parameters Via Url In Django

If you find the Django url pattern is not easy to use when you want to pass multiple request parameters via url, you can use the original method to implement it. The original method is to use url like this http://127.0.0.1:8000/dept_emp/emp_list/?dept_id=1&user_name=jerry. It just adds the parameter name and value pair after the question mark (?) of the url, and each name-value pair is separated by & mark. This article will tell you how to get the request parameter value in Django.

1. Use HttpRequest GET.

  1. Django HttpRequest GET attribute is a dictionary-like object that contains all get method request parameters and related values.
  2. You can retrieve get method request parameter’s value like the below source code. If no value is retrieved then return the default value -1.
    dept_id = request.GET.get('dept_id', -1)

2. Get Multiple Parameters Via Url Example.

  1. This example is also based on the source code of Django Bootstrap3 Example. It adds one feature, that when the user clicks the department name, it will list all the employees of that department.
  2. Below is this example demo video ( list department employees ).
  3. We need to change 3 source files in the example.

2.1 DjangoHelloWorld / dept_emp / urls.py

  1. Change employee list url pattern in dept_emp / urls.py as below.
    # request http://127.0.0.1:8000/dept_emp/emp_list will invoke the emp_list function defined in views.py.
    url(r'^emp_list/', views.emp_list, name='emp_list'),

2.2 DjangoHelloWorld / dept_emp / views.py

  1. Change the emp_list function to below code.
    @login_required()
    def emp_list(request):
        dept_id = request.GET.get('dept_id', -1)
        # list all employee.
        if dept_id == -1:
            emp_list = Employee.objects.all()
            print(emp_list)
            return render(request, 'dept_emp/emp_list.html',
                          {'emp_list': emp_list})
        # list special department employee.
        else:
            # return Department object which id equals to the dept_id
            dept_list = Department.objects.filter(id = dept_id)
    
            # create an empty employee list object.
            emp_list = list()
    
            if len(dept_list) > 0:
                dept = dept_list[0]
    
                # return the employee list of this department.
                emp_list = Employee.objects.filter(dept=dept)
    
            return render(request, 'dept_emp/emp_list.html',
                          {'emp_list': emp_list})

2.3 DjangoHelloWorld / templates / dept_emp / dept_list.html

  1. Add href link when render the department name in the loop. And add the request parameter dept_id with it’s value.
    <td><a href="{% url 'dept_emp:emp_list' %}?dept_id={{ dept.id }}">{{ dept.dept_name }}</a></td>
    {% for dept in dept_list %}
          <tr>
             <td><input type="checkbox" id="dept_{{ dept.id }}" name="dept_{{ dept.id }}"></td>
             <td>{{ dept.id }}</td>
             <td><a href="{% url 'dept_emp:emp_list' %}?dept_id={{ dept.id }}">{{ dept.dept_name }}</a></td>
             <td>{{ dept.dept_desc }}</td>
          </tr>
    {% endfor %}

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Clicky