Python Forum
Django - Catching None in Try/Except
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Django - Catching None in Try/Except
#1
Essentially, using Django, I've got a next tutorial button that finds the next tutorial by selecting an order value greater than the current tutorial, and choosing the first of the returned values.

If there is *no* next tutorial in the chapter (it will return None), where instead, it will find the next chapter.

    next_video = tutorials.filter(chapter__coursechapter__ordering=current_chapter)\
        .filter(ordering__gt=current_tutorial).first()
    if next_video is None:
        next_video = tutorials.filter(chapter__coursechapter__ordering__gt=current_chapter).filter(ordering=1)\
            .first()
The result is a simple bit of code that allows it to find the next tutorial (or) chapter if None is found, until it runs out of tutorials/chapters to look for. Once it runs out, it will again return None.

My question is if there is a way to catch the second time it returns None and do something else. So when it runs out of tutorials/chapters to loop through it can finish. My thought was a Try/Except - but I wasn't sure how to catch None in an exception?
Reply
#2
Just use some kind of switch - on/off. When the first time you get None set it to on or True or whatever and when the next time gets None check the value and take some actions. After that, you can set it back to off/False
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Django: How to automatically substitute a variable in the admin page at Django 1.11? m0ntecr1st0 3 3,315 Jun-30-2019, 12:21 AM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020