Question

When I put this line in my html template, I can successfully pass the input string via a jinja2 variable into my Python code...

<label for="firstName">First name*</label>
<input type="text" name="fname" id="firstName" value="{{ fname }}">

However, when I attempt to pass a hidden input with the following line...

<input type="hidden" name ="contact_form" value="{{ active_form }}">

... I'm not seeing the value pass back to my Python code. I've not learned Javascript yet. Is there some Javascript required to pass hidden input values? What am I missing?

Was it helpful?

Solution

I recommend using WTForms.

Example

from wtforms import TextField, validators, PasswordField, TextAreaField, HiddenField    
class ArticleCreateForm(Form):
        title = TextField('Title', [validators.Required("Please enter title.")],
                          filters=[strip_filter] )
        body = TextAreaField('Body', [validators.Required("Please enter body.")],
                             filters=[strip_filter])
        category = QuerySelectField('Category', query_factory=category_choice )
        person_name = HiddenField()

views.py

@app.route('/create', methods=['GET', 'POST'])
def article_create():
    if 'email' not in session:
        return redirect(url_for('signin'))
    person = Person.query.filter_by(email=session['email']).first()
    name = person.firstname
    article = Article()
    form = ArticleCreateForm()
    form.person_name.data = person.firstname
    if form.validate_on_submit():
        form.populate_obj(article)
        db.session.add(article)
        db.session.commit()
        return redirect(url_for('index'))
    return render_template('create.html', form=form, person=person, name=name)

OTHER TIPS

I know this is an old post but I was fooling around with a Jinja template and I wanted solve this particular issue since I was dealing with it myself. I was planning on fooling Jinja by passing in my value and adding the quote symbols (") in a concatenated string along with my Jinja variable (named "ticket_number") like so:

<input class="hidden" name="ticket_number" value={{ ticket.ticket_number + '"' }}>

That will NOT work. So I removed the concatenation and passed the variable in directly. This seemed promising. Was expecting to find the HTML code would contain the variable value without the quote symbols surrounding up but was presently surprised to find that either Jinja or Flask quoted the variable correctly with no extra add on's using the straight variable value:

<input class="hidden" name="ticket_number" value={{ ticket.ticket_number }}>

The variable itself is an integer so I confirmed if this also happens with regular alphanumerical string variables. Let me know if it works for you all-

If you are using Flask along with Jinja2 (which I would recommend), you can run code like this in the view function:

from flask import request

@app.route('/path/to/submit/page', methods=["GET","POST"])
def viewfunctionforsubmitpage():
  request.form['contact_form']

Flask is a nice, lightweight, and built using Jinja2, so that is good.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top