Visitor : 100

Read Comments

URL Parameters in GET Request

Django Concept

Parameters in the URL are very useful for simple activates like search , filter and sorting. Even though in Django this can be managed using URL config , the simplest option would be to read the URL parameters in a GET call and process the requirements. In this article explains how sort can be achieved using URL parameters in a GET call.

Sorting columns in a table is a simple requirement in most of the projects. This can be achieved using URL config in Django using

from django.urls import path

from . import views

urlpatterns = [

    path('articles/<sort_key>/', views.article_detail),

The other option would be read the GET parameter in the view.

def consolidated_portfolio_analysis(request):
    z = cache.get('sort_order')
    zsort = request.GET.get('sort_by','ztrades')
    if not(z) :
        zsort = '-' + zsort 
    elif z == 'D':
        zsort = zsort
    elif z == 'A':
        zsort = '-' + zsort 
 zresut = articles.objects.all().order_by(zsort,)
    return render (request,'equity/Equity_Report_Consolidated.html',{'obj_equity':zresult})

I have included a DB Cache object to capture the sorting order. So when the user clicks the column multiple times , the query set is sorted in ascending and descending  alternatively.

The url would look like


sort_by parameter is extracted in the view of the GET call and processed accordingly.

The syntax would be 

request.GET.get('<key in url>','<default value>')


Add Comments