Failed to pass variable to macro arguments when using Bootstrap-Flask

738 views Asked by At

I was first using Flask bootstrap and it worked

{% import "bootstrap/wtf.html" as wtf %}
<form method="POST" action="{{url_for('edit_ad', id=ad.id)}}" enctype="multipart/form-data">
    {{ form.hidden_tag() }}
    {{ wtf.form_field(form.title) }}
    {{ wtf.form_field(form.body) }}
    {{ wtf.form_field(form.img_url) }}
    {{ wtf.form_field(form.price) }}
    <div>
        <p>
            <button class="" type="submit" value="Edit">Edit</button>
        </p>
    </div>
</form>

But I wanted to change it to Bootstrap Flask and use render_form. I'm having trouble with url_for

{% from 'bootstrap/form.html' import render_form %}
{{ render_form(form, action="{{url_for('edit_ad', id=ad.id)}}", method="POST", button_map={'edit_button': 'primary'}) }}

Here is my view:
@app.route('/edit_ad/<string:id>', methods=['GET', 'POST'])
@login_required
def edit_ad(id):

# more code
    return render_template('edit_ad.html', form=form, ad=ad)

How should I pass id? Thanks

2

There are 2 answers

5
Harris Minhas On

You can pass the variable like this,

{% from 'bootstrap/form.html' import render_form %}
{{ render_form(form, action="/edit_ad/{{ ad.id }}", method="POST", button_map={'edit_button': 'primary'}) }}
0
Grey Li On

You can't and don't need to use Jinja delimiters inside other delimiters (i.e. nest the curly brackets {{ {{...}} }}).

Just pass the url_for call to the action keyword directly. No curly brackets, no quotes:

{{ render_form(form, action=url_for('edit_ad', id=ad.id), method="POST", button_map={'edit_button': 'primary'}) }}