Home > Mobile >  HTML wont pass argument to flask missing 1 required positional argument
HTML wont pass argument to flask missing 1 required positional argument

Time:04-21

I can't figure this error out, I am passing the chirp_id from this <a href> the error TypeError: createCmt() missing 1 required positional argument: 'chirp_id'

The Snippet of the code

{% block content %}
<div >
    {% if chirp %}

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

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

The URL that is called by the button is this

@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')

Does this mean that the chirp_id isn't being passed from the chirp_id='{{ chirp[0] }}' in the HTML? or I am just stupid

EDIT

this is my create.html that extends to the first HTML

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

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

{% block button %}
<div >
    <div >
        <button type="button"  name="btnCreate" id="btnCreate" onclick="createCUM()">Create</button>
    </div>
</div>
{% endblock %}

and the javascript for getting the body value

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
            }
        },
    )
}

CodePudding user response:

  1. Your @app.route seems to be lacking a paramater, would you like to do it as
@app.route("/comment/create/<chirp_id>", methods = ["GET", "POST"])
  1. Try to change your href to
<a  href="{{ url_for('createCmt', chirp_id='{}'.format(chirp[0])) }}">   Comment </a>

additional edit

I tried to run it I did this

Route for the a href

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

Route for the post

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

HTML

  <a  href="{{ url_for('createCmt', chirp_id='{}'.format(chirp_id) ) }}">   Comment </a>
  • Related