yayu
yayu

Reputation: 8098

Django capturing query parameters

I am trying to create a Django Rest Framework view that filters a queryset. The docs seem to be ok, but before that I am a little confused how I will capture the query parameters in the urlconf. So far I have only made simple confs like /app/<id>/<slug>etc. How can I capture a url like /api/model?param1=hello&param2=world so that I can use request.QUERY_PARAMS in my view, as in the docs? I was thinking of capturing the params in regular expressions (something like ../param1=(?P<param1>)) but it doesn't feel right.

Upvotes: 0

Views: 83

Answers (1)

scoopseven
scoopseven

Reputation: 1777

You probably don't want to catch them in the urlconf. Catch query params right in your view code.

def myview(request):
    if 'mygetvar' in request.GET:
        myobjects = MyObjects.objects.filter(somefield=request.GET.get('mygetvar', 'defaultvalue'))
        # do something useful

Docs

Upvotes: 1

Related Questions