HTML 不会将参数传递给缺少 1 个必需位置参数的烧瓶

HTML wont pass argument to flask missing 1 required positional argument

我无法弄清楚这个错误,我正在从这个 <a href> 传递 chirp_id 错误 TypeError: createCmt() missing 1 required positional argument: 'chirp_id'

代码片段

{% block content %}
<div class="row">
    {% if chirp %}

    <h2>{{ chirp[1] }}</h2>
    <p>
        By : {{ chirp[2] }}
    </p>
    <p>
        {{ chirp[3] }}
    </p>
       
    <div>
        <a class="btn btn-success" href="{{ url_for('createCmt', chirp_id='{{ chirp[0] }}') }}"> + Comment </a>
    </div>

    {% else %}
    <h3>
        Invalid comment
    </h3>
    {% endif %}
</div>

按钮调用的URL是这个

@app.route('/comment/create', methods=['GET', 'POST'])
def createCmt(chirp_id):
    # check if user is logged in
    if not session:
        return redirect(url_for('login'))

    if request.method == 'POST':
        data = request.get_json() or {}
        if data.get('body'):
            user_id = session.get('user_id')
            body = data.get('body', '')            

            body = body.strip()
            sql_params = (chirp_id, user_id, body)

            conn = db_connection()
            cur = conn.cursor()
            sql = """
                INSERT INTO commentary (chirp_id, user_id, body) VALUES (%d, %d, '%s')
            """ % sql_params
            cur.execute(sql)
            conn.commit()
            cur.close()
            conn.close()
            return jsonify({'status': 200, 'message': 'Success', 'redirect': '/'})

        return jsonify({'status': 500, 'message': 'No Data submitted'})

    return render_template('comment/create.html')

这是否意味着 chirp_id 未从 chirp_id='{{ chirp[0] }}' 传递 在 HTML?或者我只是愚蠢

编辑

这是我的 create.html 延伸到第一个 HTML

{% extends "comment/form.html" %}

{% block title %}
Create a Comment
{% endblock %}

{% block button %}
<div class="row">
    <div class="offset-2 col-6">
        <button type="button" class="btn btn-success" name="btnCreate" id="btnCreate" onclick="createCUM()">Create</button>
    </div>
</div>
{% endblock %}

和获取正文值的javascript

function createCUM(chirp_id) {
    var body = document.getElementById("body").value;

    axios({
        method: "POST",
        url: "/comment/create/" + chirp_id,
        data: {
            body: body,
        },
        headers: {
            "Content-Type": "application/json",
        }
    }).then(
        (response) => {
            var data = response.data;
            if (data.redirect) {
                // redirect exists, then set the URL to the redirect
                window.location.href = data.redirect;
            }

            if (data.status == 500) {
                alert(data.error);
                window.location.href = "/";  // redirect to home page
            }
        },
    )
}

  1. 你的@app.route好像缺少一个参数,你愿意这样吗
@app.route("/comment/create/<chirp_id>", methods = ["GET", "POST"])
  1. 尝试将您的 href 更改为
<a class="btn btn-success" href="{{ url_for('createCmt', chirp_id='{}'.format(chirp[0])) }}"> + Comment </a>

补充编辑

我试过 运行 我做到了

a href 的路线

@app.route("/test")
def testing():
  return render_template("testerer.html", chirp_id = 123)

post

的路线
@app.route("/comment/create/<chirp_id>", methods = ["GET", "POST"])
def createCmt(chirp_id):
  return f"{chirp_id}"

HTML

  <a class="btn btn-success" href="{{ url_for('createCmt', chirp_id='{}'.format(chirp_id) ) }}"> + Comment </a>