Better way of checking query string parmeter in Django

Question:

I have quite some integer query string params that I convert this way. Is there built in better way in python to handle this ?

current_page = request.GET.get('page')
if current_page is not None and current_page.isnumeric():
    current_page  = int(current_page )
else 
    current_page = 0
Asked By: Pit Digger

||

Answers:

An easier way might be to just try to convert it, and use 1 if it fails:

try:
    current_page = int(request.GET.get('page', 1))
except ValueError:
    current_page = 1
Answered By: Willem Van Onsem
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.