使用Python和Flask的待办事项列表,无法在我的“已完成任务”页面上反映任务

时间:2019-11-08 11:13:24

标签: python sqlite flask

我有一个使用Flask和Python和sqlite后端(SQLAlchemy)制作的Todo应用程序。我想知道如何将“主页”上标记为完成的任务“移动”到“已完成任务”页面。我是编程新手,似乎无法理解逻辑

# routes.py

from flask import render_template, flash, redirect, url_for, request
from todo import app, db
from todo.forms import NewtaskForm
from todo.models import Task


@app.route("/")
@app.route("/home", methods=['GET', 'POST'])
def home():
    tasks = Task.query.all()
    return render_template('home.html', title='home', tasks=tasks)


@app.route("/task/completed", methods=['GET'])
def Completed_tasks():
    return render_template('completed-tasks.html', title='Complete tasks')


@app.route("/task/<int:task_id>")
def task(task_id):
    task = Task.query.get_or_404(task_id)
    return render_template('task.html', title=task.title, task=task)


@app.route("/task/<int:task_id>/completed", methods=['POST'])
def task_complete(task_id):
    task = Task.query.get_or_404(task_id)
    db.session.commit()
    flash('Task has been added', 'success')
    return redirect(url_for('Completed_tasks'))
# models.py

from datetime import datetime
from todo import db

class Task(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(20), nullable=False)
    description = db.Column(db.Text)
    date_posted = db.Column(db.DateTime, default=datetime.today().date(), nullable=False)
    date_due = db.Column(db.DateTime, nullable=False)
    time = db.Column(db.Time, nullable=False)

    def __repr__(self):
        return "Task('{}', {}, {}, {})".format(self.title, self.date_posted, self.date_due, self.time)

db.create_all()
# Completed-tasks.html (not sure if you'll need this)

{% extends "layout.html" %}
{% block content %}
    {% if tasks %}
        {% for task in tasks %}
            <article class="media content-section">
                <div class="media-body">
                    <div class="article-metadata">
                        <a class="mr-2" href="#">Posted on - {{ task.date_posted.strftime('%Y-%m-%d') }}</a>
                        <a class="mr-2" href="#">Due on - {{ task.date_due.strftime('%Y-%m-%d') }}</a>
                        <a class="mr-2" href="#">Time:  {{ task.time.strftime('%H:%M') }}</a>
                    </div>
                    <h2><a class="article-title">{{ task.title }}</a></h2>
                    <p class="article-content">{{ task.description }}</p>
                </div>
            </article>
        {% endfor %}
    {% else %}
        <h1>Completed Tasks</h1>
    {% endif %}
{% endblock content %}

我需要将所选任务从本地路由“移动”到completed_tasks路由。我已经尝试了所有方法,并且所做的只是将我重定向到一个空的“已完成任务页面”,没有提交任何任务。

1 个答案:

答案 0 :(得分:0)

通过向我的模型中添加布尔值,我能够使其工作。然后使用布尔值过滤主页和完成页面的数据库。

models.py

class Task(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(20), nullable=False)
    description = db.Column(db.Text)
    date_posted = db.Column(db.DateTime, default=datetime.today().date())
    date_due = db.Column(db.DateTime)
    time = db.Column(db.Time)
    complete = db.Column(db.Boolean, nullable=False)


    def __repr__(self):
        return "Task('{}', {})".format(self.title, self.complete)

然后我根据完整的布尔值过滤了首页和完整的页面

@app.route("/")
@app.route("/home", methods=['GET', 'POST'])
def home():
    tasks = Task.query.filter_by(complete = False)
    return render_template('home.html', title='home', tasks=tasks)

@app.route("/task/<int:task_id>/completed", methods=['POST'])
def task_complete(task_id):
    task = Task.query.get_or_404(task_id)
    task.complete = True
    db.session.add(task)
    db.session.commit()
    flash('Your task has been completed', 'success')
    return redirect(url_for('Completed_tasks'))