Passing values from HTML form to Python Flask.

huangapple go评论88阅读模式
英文:

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

  1. from flask import Flask, render_template, request, redirect, url_for
  2. app = Flask(__name__)
  3. all_books = []
  4. @app.route('/')
  5. def home():
  6. return render_template('index.html')
  7. @app.route('/add', methods=['GET', 'POST'])
  8. def add():
  9. book_name = request.form['bookName']
  10. book_author = request.form['book-author']
  11. book_rating = request.form['book-rating']
  12. return f"<h1>Name: {book_name}, Author: {book_author}, Rating: {book_rating}</h1>"
  13. if __name__ == "__main__":
  14. app.run(host='0.0.0.0', debug=True)

index.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Library</title>
  6. </head>
  7. <body>
  8. <h1>My Library</h1>
  9. <ul>
  10. <li></li>
  11. </ul>
  12. <a href="{{ url_for('add') }}">Add New Book</a>
  13. </body>
  14. </html>

add.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Add Book</title>
  6. </head>
  7. <body>
  8. <form action="/add" method="post">
  9. <label>Book Name</label>
  10. <input type="text" id="bookName" name="book-name">
  11. <label>Book Author</label>
  12. <input type="text" id="book-author" name="book-author">
  13. <label>Rating</label>
  14. <input type="text" id="book-rating" name="book-rating">
  15. <button type="submit">Add Book</button>
  16. </form>
  17. </body>
  18. </html>

答案1

得分: 1

问题在于你尝试在/add路由上执行两个任务,首先尝试显示add.html页面,其次处理该页面上的表单。你需要通过创建一个单独的路由来分离这两个任务。

代码:

  1. @app.route('/added', methods=['GET', 'POST'])
  2. def added():
  3. book_name = request.form['book-name']
  4. book_author = request.form['book-author']
  5. book_rating = request.form['book-rating']
  6. return f"<h1>Name: {book_name}, Author: {book_author}, Rating: {book_rating}</h1>"

现在在add.html表单中调用/added

  1. <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:

  1. @app.route(&#39;/add&#39;)
  2. def add():
  3. return render_template(&#39;add.html&#39;)
  4. @app.route(&#39;/added&#39;, methods=[&#39;GET&#39;, &#39;POST&#39;])
  5. def added():
  6. book_name = request.form[&#39;book-name&#39;]
  7. book_author = request.form[&#39;book-author&#39;]
  8. book_rating = request.form[&#39;book-rating&#39;]
  9. return f&quot;&lt;h1&gt;Name: {book_name}, Author: {book_author}, Rating: {book_rating}&lt;/h1&gt;&quot;

Now call /added from the form in add.html:

  1. &lt;form action=&quot;/added&quot; method=&quot;POST&quot;&gt;

答案2

得分: 0

我发现了一个解决方案,而不是像@Marijn那样添加新路由。我在index.html中将路由改为动态。以下是main.py和add.html的代码:

main.py

  1. from flask import Flask, render_template, request, redirect, url_for
  2. app = Flask(__name__)
  3. all_books = []
  4. @app.route('/')
  5. def home():
  6. return render_template("index.html", books=all_books)
  7. @app.route("/add", methods=["GET", "POST"])
  8. def add():
  9. if request.method == "POST":
  10. new_book = {
  11. "title": request.form["title"],
  12. "author": request.form["author"],
  13. "rating": request.form["rating"]
  14. }
  15. all_books.append(new_book)
  16. return render_template("add.html")
  17. if __name__ == "__main__":
  18. app.run(host='0.0.0.0', debug=True)

add.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Add Book</title>
  6. </head>
  7. <body>
  8. <form action="{{ url_for('add') }}" method="POST">
  9. <label>Book Name</label>
  10. <input name="title" type="text">
  11. <label>Book Author</label>
  12. <input name="author" type="text">
  13. <label>Rating</label>
  14. <input name="rating" type="text">
  15. <button type="submit">Add Book</button>
  16. </form>
  17. </body>
  18. </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

  1. from flask import Flask, render_template, request, redirect, url_for
  2. app = Flask(__name__)
  3. all_books = []
  4. @app.route(&#39;/&#39;)
  5. def home():
  6. return render_template(&quot;index.html&quot;, books=all_books)
  7. @app.route(&quot;/add&quot;, methods=[&quot;GET&quot;, &quot;POST&quot;])
  8. def add():
  9. if request.method == &quot;POST&quot;:
  10. new_book = {
  11. &quot;title&quot;: request.form[&quot;title&quot;],
  12. &quot;author&quot;: request.form[&quot;author&quot;],
  13. &quot;rating&quot;: request.form[&quot;rating&quot;]
  14. }
  15. all_books.append(new_book)
  16. return render_template(&quot;add.html&quot;)
  17. if __name__ == &quot;__main__&quot;:
  18. app.run(host=&#39;0.0.0.0&#39;, debug=True)

add.html

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html lang=&quot;en&quot;&gt;
  3. &lt;head&gt;
  4. &lt;meta charset=&quot;UTF-8&quot;&gt;
  5. &lt;title&gt;Add Book&lt;/title&gt;
  6. &lt;/head&gt;
  7. &lt;body&gt;
  8. &lt;form action=&quot;{{ url_for(&#39;add&#39;) }}&quot; method=&quot;POST&quot;&gt;
  9. &lt;label&gt;Book Name&lt;/label&gt;
  10. &lt;input name=&quot;title&quot; type=&quot;text&quot;&gt;
  11. &lt;label&gt;Book Author&lt;/label&gt;
  12. &lt;input name=&quot;author&quot; type=&quot;text&quot;&gt;
  13. &lt;label&gt;Rating&lt;/label&gt;
  14. &lt;input name=&quot;rating&quot; type=&quot;text&quot;&gt;
  15. &lt;button type=&quot;submit&quot;&gt;Add Book&lt;/button&gt;
  16. &lt;/form&gt;
  17. &lt;/body&gt;
  18. &lt;/html&gt;

huangapple
  • 本文由 发表于 2023年4月20日 02:11:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76057643.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定