Flask - getting value from radio button? werkzeug.exceptions.BadRequestKeyError: 400 Bad Request

Flask - getting value from radio button? werkzeug.exceptions.BadRequestKeyError: 400 Bad Request

每当我尝试访问“http://127.0.0.1:5000/submissions”时,我正在尝试获取单选按钮的值并收到 BadRequestKeyError: 400

错误代码描述为:werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: 浏览器(或代理)发送了本服务器无法理解的请求。 键错误:'submission'

这里是相关代码(selected_song = requests.form["submission"]后的print(selected_song)可以忽略)

烧瓶:

@app.route("/submissions", methods=['GET', 'POST'])
@login_required
def submissions():
    user = User.query.filter_by(email=current_user.email).first_or_404()
    if user.user_type == "Artist":
        page = request.args.get('page', 1, type=int)
        submissions = Upload.query.filter_by(artist=current_user)\
            .order_by(Upload.date_submission.desc())\
            .paginate(page=page, per_page=5)        
        selected_song = request.form["submission"]
        print(selected_song)
        return render_template('submissions.html', submissions=submissions, title='Submit')
    else:
        return render_template('submissions.html')

HTML:

{% extends "layout.html" %} 
{% block content %}
    {% if submissions %}
      </div>
        {% for submission in submissions.items %}
          <form action="" method="POST">
            <article class="media content-section">
              <img class="rounded-circle article-img" src="{{ url_for('static', filename='profile_pictures/' + submission.artist.image_file) }}">
              <div class="media-body">
                <div class="article-metadata">
                  <a class="mr-2" href="{{ url_for('user_submissions', username=submission.artist.username) }}">{{ submission.artist.username }}</a>
                  <small class="text-muted">{{ submission.date_submission.strftime('%B %d, %Y') }}</small>
                </div>
                <input type="radio" name="submission" value="{{submission.id}}"><a target="_blank"></a><a class="article-title" href="{{ url_for('submission', submission_id=submission.id)  }}"> {{ submission.artists_from_song }} - {{ submission.song_name }}</a>
                <a href="{{ submission.song_link }}"><iframe src="https://open.spotify.com/embed/track/{{submission.song_uri}}" height="80" frameborder="0" allowtransparency="true" allow="encrypted-media"></iframe></a>
                <p class="article-content">Song Description: {{ submission.song_description }}</p>
              </div>
            </article>
            <input type=submit value="Submit Your Song">
          </form>
        {% endfor %}
      {% for page_num in submissions.iter_pages(left_edge=1, right_edge=1, left_current=1, right_current=2) %}
        {% if page_num %}
          {% if submissions.page == page_num %}
            <a class="btn btn-info mb-4" href="{{ url_for('submissions', page=page_num) }}">{{ page_num }} </a>
          {% else %}
            <a class="btn btn-outline-info mb-4" href="{{ url_for('submissions', page=page_num) }}">{{ page_num }} </a>
          {% endif %}
        {% else %}
        ...
        {% endif %}
      {% endfor %}
    {% else %}
      {% if current_user.is_authenticated %}
        <div>
            <p class="text-center text-muted">You're not logged in with an artist account<p>
        </div>
      {% endif %}
    {% endif %}
{% endblock content %}

问题是我没有 if 语句来检查表单是否已提交或页面是否是第一次加载。

保持 HTML 不变,Python 代码应更新如下:

@app.route("/submissions", methods=['GET', 'POST'])
@login_required
def submissions():
    user = User.query.filter_by(email=current_user.email).first_or_404()
    if user.user_type == "Artist":
        page = request.args.get('page', 1, type=int)
        submissions = Upload.query.filter_by(artist=current_user)\
            .order_by(Upload.date_submission.desc())\
            .paginate(page=page, per_page=5)    
        if request.method == 'POST':
            selected_song = request.form["submission"]
            print(selected_song)
        return render_template('submissions.html', submissions=submissions, title='Submit')
    else:
        return render_template('submissions.html')