0

My final project is a task website that allows you to create tasks, complete them, and get alerted when you haven't done a task on time. Currently, I am working on alerting when the task is due. The date due saves to my database, but when I run my for loop on index.html that iterates through the user's tasks to see if they're due that day, the loop doesn't do anything. Please tell me if you have an idea of why this is happening.

Application.py:

@app.route("/", methods=["GET", "POST"])
def index():
    if session.get("user_id") is None:
        return redirect("/login")
    if request.method == "POST":
        db.execute("DELETE FROM tasks WHERE task = :task AND user_id = :user_id", task=request.form.get("complete"), user_id=session["user_id"])
        rows=db.execute("SELECT * FROM tasks WHERE user_id = :user_id", user_id=session["user_id"])
        return render_template("index.html", rows=rows)
    else:
        timestamp=datetime.now()
        current_date=timestamp.date()
        rows=db.execute("SELECT * FROM tasks WHERE user_id = :user_id", user_id=session["user_id"])
        return render_template("index.html", rows=rows, current_date=current_date)

Index.html

{% extends "layout.html" %}

{% block title %}
    Home
{% endblock %}

{% block main %}
    <h1>MyTasks</h1>
    <center>
        {% for i in rows %}
            {% if current_date == i["date_due"] %}
                <div>Your task {{i["task"]}}, due today, has not been completed.</div>
            {% endif %
        {% endfor %}
        <a href="/add" target="_blank" class="button">New Task</a>
        <br><br><br>
        {% for i in rows %}
            <br>
            <div type="text">-{{i["task"]}}</div>
        {% endfor %}
        <br><br>
        <form method="POST" class ="ey">
            <select name="complete">
            <option>~Select~</option>
            {% for i in rows %}
            <option>{{i["task"]}}</option>
            {% endfor %}
        </select>
        <button name="complete_button" class="button">Complete</button>
        </form>
    </center>
{% endblock %}
1

It's running but nothing "matches" (ie is true) because of the date formats. Assuming "due_date" is TEXT in the db with the format "YYYY-MM-DD", current_date needs to be in the same format. That can be accomplished by changing this current_date=timestamp.date() to this current_date=str(timestamp.date()). That will coerce the datetime object to the appropriate string representation.

If due_date includes a time component, review the sqlite3 Date and Time Functions doc, specifically strftime to get the date in the appropriate format. Or it can be sliced and diced in python.

3
  • due_date is in the date category in my database, not TEXT. Isn't current_date already in that same format?
    – mkg15
    Oct 29 '20 at 20:30
  • sqlite doesn't have a native date category. Review the cited doc. current_date is a python datetime object. they are not in the same format. Oct 30 '20 at 10:34
  • this solved my problem. Thank you!
    – mkg15
    Nov 3 '20 at 21:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .