简体   繁体   中英

How to return ordered objects from django CBV(ListView) when you click a button or link in the template

So I'm building an e-commerce store with Django(First project after learning). I need to click on Sort in the template, and have the CBV return an object that's ordered by either, price, or whatever field I specify in the request. This is what I have so far

Template Sort by Lowest Price

View

class ClotheListView(ListView):
model = Clothe
paginate_by = 8

def get_filter_param(self):
    # Grab the absolute url and then retrieve the filter param
    filter_param = self.request.path.split("/")[-1]
    return filter_param

def get_queryset(self):
    filter_param = self.get_filter_param()
    if(filter_param != ""):
        queryset = self.model.objects.filter(cloth_gender=filter_param)
    else:
        queryset = self.model.objects.all()
    return queryset

    return clothes_filtered_list.qs

def get_ordering(self):
    ordering = self.request.GET.get('ordering', '-cloth_price')
    return ordering

def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    return context

Url.py

urlpatterns = [
path('', views.ClotheListView.as_view(), name="clothe_list"),
path('<slug:slug>', views.ClotheListView.as_view(),
     name="clothe_list_category"),
path('<int:pk>/', views.ClotheDetailView.as_view(), name="clothe_detail")

]

ok, so this is how I did it:

Template

 <span class="float-end d-none d-lg-block mt-3"> Sort by: Price - <a href="{{request.path}}?ordering=-cloth_price">High To Low</a> | Price: <a href="{{request.path}}?ordering=+cloth_price">Low to High</a> </span>

View

class ClotheListView(ListView):

model = Clothe
paginate_by = 8

def get_filter_param(self):
    # Grab the absolute url and then retrieve the filter param
    filter_param = self.request.path.split("/")[-1]
    return filter_param

def get_queryset(self):
    # code for price sorting
    default_order = "cloth_name"
    order_param = ""
    user_filter = ""
    try:
        order_param = self.request.GET.get('ordering').strip()
    except:
        pass

    try:
        user_filter = self.request.GET.get('filter').strip()
    except:
        pass

    order_by = order_param or default_order
    # End of sorting code

    filter_param = self.get_filter_param()
    if(filter_param != "" or not filter_param):
        if(user_filter != ""):
            queryset = self.model.objects.filter(
                cloth_gender=filter_param, cloth_category=user_filter)
        else:
            queryset = self.model.objects.filter(
                cloth_gender=filter_param)
    else:
        queryset = self.model.objects.all()
    return queryset.order_by(order_by)

def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    return context

url.py

urlpatterns = [
path('', views.ClotheListView.as_view(), name="clothe_list"),
path('<slug:slug>', views.ClotheListView.as_view(),
     name="clothe_list_category"),
path('<int:pk>/', views.ClotheDetailView.as_view(), name="clothe_detail")
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM