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
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
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
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