简体   繁体   中英

Flask-WTForms and hidden fields

I'm trying to generate a series of forms, each of them containing a specific value within a hidden field.

Here is the code of my viewer template:

<div class="container">
       <form method="POST" action="{{ url_for('list') }}">
           <dl>
                        {{ form.csrf_token }}
                        {{ form.wishlist.label }}
                        {{ form.wishlist }}
                        {{ form.item_url_field(value="" ) }}

                      <input class="btn btn-primary" type="submit" value="Login">
                      </dl>
                  </form>
              </div>

The specific issue I'm struggling with is at this level:

{{ form.item_url_field(value="" ) }}

If I try something like this:

   {{ form.item_url_field(value="{{row["item_url"]}}" ) }}

I get this error message:

TemplateSyntaxError: expected token ',', got 'item_url'

Isn't it possible to pass a string value automatically to an hidden field before it's being built?

Thanks for your feedback

@app.route
def index():
    class someObj:
        def someFunc(someArg):
            return 10 + someArg
    return render_template("template.html", obj=someObj(), arg=25)

-------------
<div>
    {{ obj.someFunc(someArg=arg) }}
</div>

The above will display '35' in the HTML. In general when inside the curly brackets you do no need to rewite them again. Ie don't do this :

{{ obj.someFunc(someArg={{ arg }}) }}

Your error is because jinja 2 thought the value you passed to the value argument was "{{row[" and after that it expected a comma separator but instead received item_url.

{{ form.item_url_field(value="{{row["item_url"]}}" ) }}

should be

{{ form.item_url_field(value=row['item_url']) }}

(if indeed row is a dict that has been injected into the template scope)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM