Flask采暖系统配置器POST请求无法跳转至/cart路由及TypeError问题求助
Hey there, let's break down what's going wrong here and fix it step by step—you're so close to wrapping this up!
The Core Issue: Broken Form Submission
The biggest problem is a tiny syntax error in your search.html template that's causing the "Choose" button to submit to the wrong route. Look at your form tag:
<form action "/cart" method="post">
You're missing an equals sign (=) after action. Browsers can't parse this correctly, so they default to submitting the form to the current page—the /search route—instead of /cart. That's why you're hitting the TypeError when you click "Choose": the /search route tries to read the output parameter, which doesn't exist in this form submission.
Step 1: Fix the Form Tag
Update the form in search.html to use the correct syntax:
<form action="/cart" method="post"> <input name="bom_id" type="hidden" value="{{ cascades["id"] }}"> <input class="btn btn-primary" type="submit" value="Choose"> </form>
This will make the form submit directly to /cart when clicked, which is exactly what you want.
Step 2: Add Guard Rails to the /search Route
Even after fixing the form, it's smart to add checks to prevent the NoneType error from happening in other scenarios (like someone manually visiting /search without submitting the form). Update your search function:
@app.route("/search", methods=["GET", "POST"]) def search(): # Only process POST requests (from the initial form submission) if request.method != "POST": return redirect(url_for("index")) output = request.form.get("output") # Validate output exists and is a number if not output or not output.strip().isdigit(): return render_template("index.html", error="Please enter a valid numeric output value") hydraulic = request.form.get("hydraulic") layout = request.form.get("layout") # Validate required dropdowns are selected if not hydraulic or not layout: return render_template("index.html", error="Please select hydraulic and layout options") try: min_output = int(output) - 15 max_output = int(output) + 15 except ValueError: return render_template("index.html", error="Please enter a valid number for output") cascades = db.execute( "SELECT * FROM cascades WHERE hydraulic = ? AND layout = ? AND output BETWEEN ? and ?", hydraulic, layout, min_output, max_output ) return render_template("search.html", cascades=cascades)
These checks ensure that only valid submissions make it through to the database query, and give users clear feedback if they miss a field.
Step 3: Hardening the /cart Route
To fix the "no data displayed" issue you saw earlier, add validation to the cart route to ensure the bom_id is passed correctly and handle cases where no BOM is found:
@app.route("/cart", methods=["GET", "POST"]) def cart(): # Only process POST requests (from the "Choose" button) if request.method != "POST": return redirect(url_for("index")) bom_id = request.form.get("bom_id") if not bom_id: return render_template("index.html", error="Invalid system pack selection") bom = db.execute("SELECT * FROM bom WHERE bom_id = ?", bom_id) # Handle cases where no BOM matches the ID if not bom: return render_template("bom.html", bom=[], error="No BOM found for this system pack") return render_template("bom.html", bom=bom)
You'll also want to update bom.html to display the error message if it exists:
{% extends "layout.html" %} {% block body %} {% if error %} <div class="alert alert-danger">{{ error }}</div> {% endif %} <h3>System Pack BOM</h3> <!-- Your BOM table/listing here --> {% endblock %}
Testing the Fix
- Restart your Flask app
- Submit the initial form with a valid output value (like 200)
- Click "Choose" on any system pack option—you should now be redirected to
/cartand see the corresponding BOM
With these changes, your app should behave exactly as you intended: showing system pack options, then loading the correct BOM when a user selects one.
内容的提问来源于stack exchange,提问作者Michael Berry

