sivaperumal10000
sivaperumal10000

Reputation: 69

How to get the limit value from the django API URL

from the below Url, I need the limit, to do some operations in my view

http://127.0.0.1:8000/emp/?limit=10&offset=5

I tried using the below things but no luck

 if request.limit>5:
        do something...

     if kwargs['limit'] >5:
        do something....

Upvotes: 0

Views: 435

Answers (2)

Kumpelinus
Kumpelinus

Reputation: 660

As documented in the Django docs you should use request.GET

to get all http Parameters as an QueryDict object.

You then just need to get the key 'limit' from that Object as stated here.

In your example:

request.GET.get('limit')

If you would rather have a default value than receive an error message if the key does not exist you should use:

request.GET.get('limit', 'default value')

Complete Code:

 if request.GET.get('limit') > 5:
        #do something...
 else:
        #do other things

Upvotes: 2

Sneil
Sneil

Reputation: 154

Like tushortz said in his comment this is what you should do:

limit = request.GET.get('limit')
if limit > 5:
    #Do something here
else:
    #Do something else

You could also directly compare request.GET.get('limit') with your value.

I just like to assign them to a variable, if I need it somewhere later.

Upvotes: 1

Related Questions