Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
127 views
in Technique[技术] by (71.8m points)

python - Unable to add a task to todo list

When I try to add a new task on my website I get the message "POST /templates/todo" Error (404): "Not found" and cannot find what is wrong with the code. Can anyone help me figure it out? It was done on cs50ide software. If anyone could inform me whether I am able to create a functioning link for my website (I use python, flask, html and css) I would be very grateful! Thank you so much

add.html code:

<!DOCTYPE html>

<html lang="en">
    <head>
        <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet">
        <link href="/static/css/styles.css" rel="stylesheet">
        <title>
          Add a New Task :)
        </title>
    </head>
    <body>
        <h1 class = "aligncenter">
            <img class = "img1" src = "/static/images/logocarol.jpg" alt = "Logo" height = "200" width = "550"/>    
        </h1>
        <h1 class="centergeneral fontsize">
            Add any goals, dreams and aspirations you might have here:
        </h1>
        <form class="aligncenter" action="todo" method="POST">
            <input id="task" name="task" type="text" placeholder="New Task :)">   
            <input id="submit" type="submit" disabled>
        </form>
        <script>
            document.querySelector('#task').onkeyup = function(){
                if (document.querySelector('#task').value === ''){
                    document.querySelector('#submit').disabled = true;
                } else {
                     document.querySelector('#submit').disabled = false;
                }
            }
        </script>
        
        <form action="/">
            <button type="submit" id = "back" class="btn btn-info margin"> BACK TO HOMEPAGE </button> <br> <br>
        </form>
        <form action="todo">
            <button type="submit" id = "back" class="btn btn-outline-info margin"> BACK TO TO DO LIST </button> <br> <br>
        </form>
    </body>

todo.html code:

<!DOCTYPE html>

<html lang="en">
    <head>
        <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet">
        <link href="/static/css/styles.css" rel="stylesheet">
        <title>
          To Do List! :)
        </title>
    </head>
    <body>
        <h1 class = "aligncenter">
            <img class = "img1" src = "/static/images/logocarol.jpg" alt = "Logo" height = "200" width = "550"/>    
        </h1>
        <h1 class="fonts centergeneral"> To Do List </h1>
        <h2 class="fs-4 centergeneral"> What I Want to Achieve: </h2> <br>
        <ul class="listcenter">
            <script>
            {%for todo in todos%}
                <li> {{ todo }} <input type="checkbox" id="checkbox1"> </li> 
            {%endfor%}
            </script>
        </ul>
        
        <a class="btn btn-outline-info margin" href = "add"> Add a New Task</a>
        <a class="btn btn-outline-info" href = "clear"> Clear Tasks </a> <br>
        <div class="backbuttons">
        <form action="/">
            <button type="submit" id = "back" class="btn btn-info"> BACK TO HOMEPAGE </button> <br> <br>
        </form>
        </div>

application.py code:

from flask import Flask, render_template, send_from_directory, request, redirect, session
from flask_session import Session
from cs50 import SQL

app = Flask(__name__)
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/f1inschools')
def f1():
    return render_template('f1inschools.html')
    
@app.route('/pdwt')
def pdwt():
    return render_template('pdwt.html')
    
@app.route('/pros')
def pros():
    return render_template('pros.html')
    
@app.route('/bookrecommendations')
def books():
    return render_template('bookrecs.html')
    
@app.route('/todolist')
def todo():
    if "todos" not in session:
        session["todos"] = []
    return render_template('todo.html', todos=session["todos"])
        
@app.route('/clear')
def clear():
    return redirect("/todolist")
    session["todos"] = []
        
@app.route('/add', methods=["GET", "POST"])
def add():
    if request.method == "GET":
       return render_template("add.html")
    else:
        todo = request.form.get("task")
        session["todos"].append(todo)
        return redirect("/todolist")
question from:https://stackoverflow.com/questions/66056428/unable-to-add-a-task-to-todo-list

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The function for adding tasks is at app.route("/add",methods=["GET","POST"]), but your form in the HTML has action="todo", so your form tries to send data to /todo which is nonexistent. To fix, simply change action="todo" to action="add".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...