jman
jman

Reputation: 13

Flask url_for not passing parameters

I have the following code in Html:

{% for i in events %}
        <li>
            <strong>{{i.event_title}}</strong><br />
            {{i.descript}} <br />
            {{i.customer_id}}<br/>
            <small>{{i.date}}</small>
            </br>
            <a href="{{url_for('cancel_event',ID=i.event_id)}}">Cancel this event</a>
        </li>
    {% else %}
        <li><strong>You have no events scheduled</strong>
    {% endfor %}

However I get the following error:canel_event missing 1 required positional argument: 'ID'. Here's the flask code:

@app.route('/cancel/')
def cancel_event(ID):
    if not ID:
        abort(401)
    Event.query.filter_by(Event.event_id == ID).delete()
    db.session.commit()
    return render_template('cancel.html')

I have no idea what I'm doing wrong. Please help

Upvotes: 1

Views: 465

Answers (1)

hxysayhi
hxysayhi

Reputation: 1987

You should give your ID in request by post or get method.Then take it from request.For example,by post:

@app.route('/cancel/', method=['POST','GET'])
def cancel_event():
    ID = None
    if request.method == 'POST':    # get ID in the request
        ID = request.files['ID']
    if not ID:
        abort(401)
    Event.query.filter_by(Event.event_id == ID).delete()
    db.session.commit()
    return render_template('cancel.html')

read this may be help:http://flask.pocoo.org/docs/0.12/quickstart/#accessing-request-data

Upvotes: 2

Related Questions