'Django Validate URLs in a List

I have an emtpy list called 'to_be_directed' that I save in direct.py file. I store all the previous links that user visited in that list up to 10 times.

I save that list to redirect the user. However some links might be dead. Therefore, I like to check the links in the list with an order and redirect the user from latest proper link. However I see that requests library doing it multiple times. Although there are 1 link in the list, I see that it's going up to 10 times.

How can I manage to redirect the users from latest active link in the list ?

views.py

from . import direct
# here I save the visited links in to_be_directed list
previous = self.request.META.get('HTTP_REFERER')
direct.to_be_directed.insert(0, previous)
if len(direct.to_be_directed) >= 10:
    direct.to_be_directed.pop()

# here I check the links if they are dead;
import requests
for i in direct.to_be_directed:
    print('***************** i printed: ', i)
    try:
        print('************** status: ', requests.get(i).status_code)
    except:
        print('************ ERR')

direct.py

to_be_directed = []

outcome of prints

[10/Mar/2022 14:29:55] "GET /actions/acbadem-admin-15f0ed1d/ HTTP/1.1" 200 68324
************** status:  200
***************** i printed:  None
************ ERR
***************** i printed:  None
************ ERR
***************** i printed:  None
************ ERR
***************** i printed:  None
************ ERR
***************** i printed:  None
************ ERR
***************** i printed:  None
************ ERR
***************** i printed:  None
************ ERR
***************** i printed:  None
************ ERR


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source