英文:
Passing values from HTML form to Python Flask
问题
以下是您要翻译的内容:
"I've already seen this question, but my code still doesn't work. I have 3 files (main.py and add.html are important here).
The user enters book name, author and rating (in '/add'), and the given values should be passed to main.py and printed out. But it gives me a 400 error, even though I gave the needed values in add.html and in main.py.
Can someone help me find what's wrong?
Thanks."
请注意,这是您提供的英文文本,已经没有代码部分了。如果您需要对特定部分进行翻译,请提供相应的文本。
英文:
I've already seen this question, but my code still doesn't work. I have 3 files (main.py and add.html are important here).
The user enters book name, author and rating (in '/add'), and the given values should be passed to main.py and printed out. But it gives me an 400 error, even thou I gave the needed values in add.html and in main.py.
Can someone help me find what's wrong?
Thanks.
main.py
from flask import Flask, render_template, request, redirect, url_for
app = Flask(__name__)
all_books = []
@app.route('/')
def home():
return render_template('index.html')
@app.route('/add', methods=['GET', 'POST'])
def add():
book_name = request.form['bookName']
book_author = request.form['book-author']
book_rating = request.form['book-rating']
return f"<h1>Name: {book_name}, Author: {book_author}, Rating: {book_rating}</h1>"
if __name__ == "__main__":
app.run(host='0.0.0.0', debug=True)
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Library</title>
</head>
<body>
<h1>My Library</h1>
<ul>
<li></li>
</ul>
<a href="{{ url_for('add') }}">Add New Book</a>
</body>
</html>
add.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Add Book</title>
</head>
<body>
<form action="/add" method="post">
<label>Book Name</label>
<input type="text" id="bookName" name="book-name">
<label>Book Author</label>
<input type="text" id="book-author" name="book-author">
<label>Rating</label>
<input type="text" id="book-rating" name="book-rating">
<button type="submit">Add Book</button>
</form>
</body>
</html>
答案1
得分: 1
问题在于你尝试在/add
路由上执行两个任务,首先尝试显示add.html
页面,其次处理该页面上的表单。你需要通过创建一个单独的路由来分离这两个任务。
代码:
@app.route('/added', methods=['GET', 'POST'])
def added():
book_name = request.form['book-name']
book_author = request.form['book-author']
book_rating = request.form['book-rating']
return f"<h1>Name: {book_name}, Author: {book_author}, Rating: {book_rating}</h1>"
现在在add.html
表单中调用/added
:
<form action="/added" method="POST">
英文:
The problem is that you try to use the /add
route for two things, first trying to display the add.html
page and second processing the form on that page. You need to separate these two by creating a separate route for displaying the page.
Code:
@app.route('/add')
def add():
return render_template('add.html')
@app.route('/added', methods=['GET', 'POST'])
def added():
book_name = request.form['book-name']
book_author = request.form['book-author']
book_rating = request.form['book-rating']
return f"<h1>Name: {book_name}, Author: {book_author}, Rating: {book_rating}</h1>"
Now call /added
from the form in add.html
:
<form action="/added" method="POST">
答案2
得分: 0
我发现了一个解决方案,而不是像@Marijn那样添加新路由。我在index.html中将路由改为动态。以下是main.py和add.html的代码:
main.py
from flask import Flask, render_template, request, redirect, url_for
app = Flask(__name__)
all_books = []
@app.route('/')
def home():
return render_template("index.html", books=all_books)
@app.route("/add", methods=["GET", "POST"])
def add():
if request.method == "POST":
new_book = {
"title": request.form["title"],
"author": request.form["author"],
"rating": request.form["rating"]
}
all_books.append(new_book)
return render_template("add.html")
if __name__ == "__main__":
app.run(host='0.0.0.0', debug=True)
add.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Add Book</title>
</head>
<body>
<form action="{{ url_for('add') }}" method="POST">
<label>Book Name</label>
<input name="title" type="text">
<label>Book Author</label>
<input name="author" type="text">
<label>Rating</label>
<input name="rating" type="text">
<button type="submit">Add Book</button>
</form>
</body>
</html>
英文:
So I found out a solution to the problem without adding a new route as @Marijn did. I changed the route in index.html to be dynamic. Here is the code for main.py and add.html:
I also changed the part where instead of printing it out it adds it as a dictionary to a list (this doesn't have anything to do with the question so don't mind that part).
main.py
from flask import Flask, render_template, request, redirect, url_for
app = Flask(__name__)
all_books = []
@app.route('/')
def home():
return render_template("index.html", books=all_books)
@app.route("/add", methods=["GET", "POST"])
def add():
if request.method == "POST":
new_book = {
"title": request.form["title"],
"author": request.form["author"],
"rating": request.form["rating"]
}
all_books.append(new_book)
return render_template("add.html")
if __name__ == "__main__":
app.run(host='0.0.0.0', debug=True)
add.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Add Book</title>
</head>
<body>
<form action="{{ url_for('add') }}" method="POST">
<label>Book Name</label>
<input name="title" type="text">
<label>Book Author</label>
<input name="author" type="text">
<label>Rating</label>
<input name="rating" type="text">
<button type="submit">Add Book</button>
</form>
</body>
</html>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论