繁体   English   中英

无法将任务添加到待办事项列表

[英]Unable to add task to todo list

当我尝试在我的网站上添加新任务时,我收到消息“POST /templates/todo”错误(404):“未找到”并且找不到代码有什么问题。 谁能帮我弄清楚? 它是在cs50ide软件上完成的。 如果有人也可以告诉我是否可以为我的网站创建一个有效的链接(我使用 python、flask、html 和 css),我将不胜感激。 非常感谢(奇怪的是它之前工作......)

add.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 的代码:


<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> 

应用代码.py

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")```

您在错误的 URL 上提交了表格。

<form class="aligncenter" action="todo" method="post"> 

应该,

<form class="aligncenter" action="/todolist" method="post"> 

如果您的所有模板都存储在模板文件夹中,请将此更改包含在app中。

app = Flask(__name__, template_folder="templates", static_folder='static')

您还必须将提交表单的 url 更改为/todolist并在application.py中进行此更改

@app.route('/todolist', methods=["GET", "POST"])

地址:html

        <form class="aligncenter" action="/todolist" method="POST"> 
            <input id="task" name="task" type="text" placeholder="New Task :)">   
            <input id="submit" type="submit" disabled>
        </form>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM