You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flask+Python技术问题:下拉菜单数据无法在结果页显示

Fixing Dropdown Value Display Issues in Flask

Hey there! Let's troubleshoot why your dropdown value isn't showing up and throwing errors while your input field works fine. This is a super common issue in Flask forms, and it almost always comes down to small oversights in either your HTML template or backend handling.

Common Causes & Fixes

1. Missing name or value Attributes in Your Dropdown

First, double-check your HTML form—your dropdown (<select>) must have a name attribute matching what you're trying to fetch in Flask, and every <option> needs a value attribute. Without these, Flask can't capture the selected value.

Here's a corrected example of your index.html form:

<form action="/results" method="POST">
  <!-- Working input field -->
  <label for="user_name">Name:</label>
  <input type="text" id="user_name" name="name" required>

  <!-- Fixed dropdown -->
  <label for="user_choice">Your Choice:</label>
  <select id="user_choice" name="dropdown_value" required>
    <option value="">Select an option</option> <!-- Optional default, helps with validation -->
    <option value="option1">First Option</option>
    <option value="option2">Second Option</option>
    <option value="option3">Third Option</option>
  </select>

  <button type="submit">Submit</button>
</form>

2. Incorrectly Fetching the Value in Flask

If your HTML is set up right, the next issue is probably how you're accessing the dropdown value in your backend. Avoid using direct indexing like request.form['dropdown_value']—this will throw a KeyError if the value is missing (e.g., user didn't select an option). Instead, use request.form.get() with a default value.

Here's the fixed /results route:

from flask import Flask, render_template, redirect, request

app = Flask(__name__)

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

@app.route('/results', methods=['GET','POST'])
def results(): 
    if request.method == 'POST':
        # Safely get form data with fallbacks
        user_name = request.form.get('name', 'Unknown')
        dropdown_selection = request.form.get('dropdown_value', 'No option selected')

        # Pass both values to the results template
        return render_template('results.html', name=user_name, dropdown_val=dropdown_selection)
    
    # Redirect GET requests back to the form
    return redirect('/')

3. Results Template Setup

Make sure your results.html correctly references the variable you're passing from Flask:

<h2>Your Submission</h2>
<p>Name: {{ name }}</p>
<p>Selected Option: {{ dropdown_val }}</p>

Key Takeaways

  • Always match the name attribute in your <select> tag to the key you use in request.form.get()
  • Never skip the value attribute on <option> tags—even if it's the same as the option text
  • Use request.form.get() instead of direct dictionary access to avoid runtime errors
  • Ensure your form uses method="POST" and your Flask route includes methods=['GET','POST']

内容的提问来源于stack exchange,提问作者Alphie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:53:45