0

While trying to build my first django project, I am stuck at passing values for product filtering. Every feature which has value 1 passed to it, will be an active filter, and others will be 0, i.e. inactive. urls.py:

urlpatterns=[
path('women/<str:category>/filter?pricefrom=<int:min>&to=<int:max>&size?s=<int:s>&m=<int:m>&l=<int:l>&xl=<int:xl>&xxl=<int:xxl>&xxxl=<int:xxxl>&color?blue=<int:blue>&red=<int:red>&yellow=<int:yellow>&black=<int:black>&white=<int:white>&green=<int:green>&brown=<int:brown>',views.women_category,name='women category page with filters'),
]

views.py:

def women_category(request,category,id=None,flag=None,min=None,max=None,s=None,m=None,l=None,xl=None,xxl=None,xxxl=None,blue=None,red=None,yellow=None,black=None,white=None,green=None,brown=None):
#product filter logics here

I want that the user should be able to use any possible combination of filters, with the filters being: Price: min,max Size: S,M,L,XL,XXL,XXXL color:yellow,blue,green,...etc

I don't know how to use Regex in urls, so please help me convert my url into a regex url so that all filter variables are optional to be passed through the fronted.

2 Answers2

0

There are a few ways that you can add optional parameters You can use regex or add multiple patterns. you can see examples here: Optional get parameters in django?

But in your case which is using get parameters to do filters, the better option would be handling it on the view. since you have a lot of parameters, it takes a lot of duplicated patterns that differ so little and if you go with regex, it will be a pretty long regex or you have to do validations on your views if you go with a simple regex. so instead just get the values on your view and do the filtering there:

def women_category(request,category):
    min_value = request.GET.get("min")
    max_value = request.GET.get("max")
    flag = request.GET.get("flag")
              ...

you can add or remove any parameter at any time without making a mess in your URL path file.

Navid Zarepak
  • 4,148
  • 1
  • 12
  • 26
  • I could use GET method by passing all the arguments as form inputs. But the issue is, if I keep a form in any html page, it will result to a 'confirm form resubmission' pop-up in the browser whenever user will try to refresh the produts page,which I don't need in a women clothes catalog, isn't it? – Kshitiz Dewani May 08 '20 at 16:44
  • I don't know what a form has to do anything with this. but `request.GET` is basically you URL parameters and there is no difference between getting those in your view or adding URL or regex. the result would be exactly the same. – Navid Zarepak May 08 '20 at 18:06
0

You do not need, nor should you, match query parameters as part of the URL. Those are available in Django through request.GET.

Side note: I think it's adviced to use a <slug:category> to match that part of the URL, if possible. I'm not quite sure will stop at /.

urls.py:

urlpatterns = [
    path('women/<slug:category>/filter',views.women_category,name='women category page with filters'),
]

views.py:

def women_category(request,category):
    id = request.GET.get('id')  # ... etc
    # product filter logics here
Johan Schiff
  • 661
  • 4
  • 13