'Python/Flask CRUD application: How do I load my Show All (dashboard) page with a blank table before user has added data to SQL database?

I built out my app while logged in with my first user. Now that I'm trying to register another user, I run into this error code: IndexError: tuple index out of range. I also get this error if I delete all tasks from SQL posted by the original user and sign in with my original user again. Hope this is clear.

HTML:

    <table style="margin: 0 auto;">
        <thead>
            <th>Title</th>
            <th>Details</th>
            <th>Due Date</th>
            <th>Actions</th>
        </thead>
        {% for t in one_user_tasks.tasks %}
        <tr>
            <td>{{ t.title }}</td>
            <td>{{ t.details }}</td>
            <td>{{ t.due_date.month }}/{{ t.due_date.day }}/{{ t.due_date.year }}</td>
            <td><button class="edit_button"><a href="/edit/{{ t.id }}">Edit</a></button> | <button class="delete_button"><a href="/delete/{{ t.id }}">Remove</a></button></td>
        </tr>
        {% endfor %}
    </table><br><br>
    <div style="text-align: center;">
        <button class="add_task_button"><a href="/addTask">Add New Task</a></button>
    </div>

Dashboard route:

@app.route("/dashboard")
def dashboard():
    if "user_id" not in session:
        flash("User must be logged in!")
        return redirect("/")
    else:
        data = {
        "id" : session["user_id"]
        }
        one_user_tasks = Task.users_tasks(data)
        return render_template("dashboard.html", one_user_tasks = one_user_tasks)

Method in task.py to get user's tasks:

@classmethod
    def users_tasks(cls, data):
        query = "SELECT * from users JOIN tasks ON users.id = tasks.user_id WHERE users.id = %(id)s"
        users_tasks_db = connectToMySQL("todo_schema").query_db(query, data)
        user = User(users_tasks_db[0])

        for tasks in users_tasks_db:
            task_data = {
                "id" : tasks["tasks.id"],
                "title" : tasks["title"],
                "due_date" : tasks["due_date"],
                "details" : tasks["details"],
                "created_at" : tasks["created_at"],
                "updated_at" : tasks["updated_at"],
                "user_id" : tasks["user_id"]
            }
            user.tasks.append(Task(task_data))
        return user


Solution 1:[1]

Without the full Traceback error, I'm guessing the problem is here:

user = User(users_tasks_db[0])

If you have no rows being pulled from SQL then you'd be getting the IndexError. One potential solution would be to test if you have any results:

if not users_tasks_db:
    # create your user object as it should look with no tasks
    user = a_function_to_deal_with_a_user_with_no_tasks()
else:
    user = User(users_tasks_db[0])

    for tasks in users_tasks_db:
        task_data = {
            "id" : tasks["tasks.id"],
            "title" : tasks["title"],
            "due_date" : tasks["due_date"],
            "details" : tasks["details"],
            "created_at" : tasks["created_at"],
            "updated_at" : tasks["updated_at"],
            "user_id" : tasks["user_id"]
        }
        user.tasks.append(Task(task_data))
return user

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 PGHE