Home > Software design >  Django IndexError: list index out of range error
Django IndexError: list index out of range error

Time:02-19

I am trying out this code i found online. When i tried commands like runserver, migrate and make migrations. All of them showed this error

Traceback (most recent call last):
  File "C:\Django\OLD\ToDo_Application-master\ToDo_Application-master\todos\views.py", line 129, in check_time
    task()
  File "C:\Django\OLD\ToDo_Application-master\ToDo_Application-master\todos\views.py", line 155, in is_expired
    if splited_notification_time[1] == "minutes":
IndexError: list index out of range

CODE OF is_expired :

def is_expired():
    connection = sqlite3.connect('db.sqlite3')
    cursor = connection.cursor()
    cursor.execute(
        " SELECT * FROM todos_todo where email_notification != '' AND notification_time != 'None' AND sent_reminder == 'False' ")
    rows = cursor.fetchall()
    todo_notify_time = 0

    for row in rows:
        todo_item_id = row[0]
        due_date_in_ms = int(datetime.fromisoformat(row[3]).timestamp() * 1000)
        current_date = int(datetime.now().timestamp() * 1000)
        splited_notification_time = str(row[6]).split(" ")
        receiver_email = row[5]
        sent_reminder = row[7]
        date_in_pst = due_date_in_ms - (7 * 60 * 60 * 1000)
        time_remaining = date_in_pst - current_date

        if splited_notification_time[0] != "None":
            if splited_notification_time[1] == "minutes":
                todo_notify_time = int(
                    (splited_notification_time[0])) * 60 * 1000
            elif splited_notification_time[1] == "hours":
                todo_notify_time = int(
                    (splited_notification_time[0])) * 60 * 60 * 1000
            elif splited_notification_time[1] == "day":
                todo_notify_time = int(
                    (splited_notification_time[0])) * 60 * 60 * 24 * 1000

        if time_remaining <= todo_notify_time:
            todo_item_expire = "Your todo_item name - "   \
                str(row[1])   " will expire in "   str(row[6])   "! "

            send_mail(
                'Todo_Notification',
                todo_item_expire,
                'noreply@todo_application.ca',
                [receiver_email],
                fail_silently=False,
            )

            selected_todo_item = get_object_or_404(Todo, pk=int(todo_item_id))
            selected_todo_item.sent_reminder = "True"
            selected_todo_item.save()

There is one line in the code which is showing an error as well. I have pasted the code below with the error.

CODE :

<script>
     timer_interval("{{todo.id}}" , "{{ todo.due_date|date:"M d, Y H:i:s" }}");
</script>

ERROR :

',' expected.javascript

CodePudding user response:

Check what splited_notification_time is returning.

It appears that there is no " " in row[6] resulting in splited_notification_time being a list of length 1.

Therefore, there will be no second element (which you are trying to access), giving you the error.

CodePudding user response:

First, Alec Hamilton's answer is correct, and I missed it thinking you would see just one character in a string.

I'm not sure what splited_notification_time is supposed to return, but since it is returning ['False'] try

if splited_notification_time[0] != "False":

Perhaps it's just me, but your query looks like Flask, as opposed to Django.

connection = sqlite3.connect('db.sqlite3')
cursor = connection.cursor()
cursor.execute(
    " SELECT * FROM todos_todo where email_notification != '' AND notification_time != 'None' AND sent_reminder == 'False' ")
rows = cursor.fetchall()
print(rows)    # WHAT DOES THIS PRINT?

Try printing rows. Is this what you expect? Perhaps the issue is the query itself? If you need to use a query like that without using the Django ORM, then you could use a raw SQL in Django.

Finally to make my answer complete, the answer to your second error, which you have already resolved, as I mentioned in my comment is to remove the double quotes within the double quotes in your HTML, like this:

<!-- Change -->
    timer_interval("{{todo.id}}" , "{{ todo.due_date|date:"M d, Y H:i:s" }}");
<!-- to -->
    timer_interval("{{todo.id}}" , '{{ todo.due_date|date:"M d, Y H:i:s" }}');
  • Related