Taras
Taras

Reputation: 77

Put text into input html field using Flask

I know I can easily extract data from html input tag using request.form['email'] after POST method. Is there a way to populate this particular input tag with something like:

request.form['email'] = current_user.email

which obviously throws 'ImmutableMultiDict' objects are immutable? (Without using FlaskWTF). Any help would be much appreciated!

Upvotes: 0

Views: 1249

Answers (2)

Taras
Taras

Reputation: 77

Actually, this could be easily done using value attribute, e.g.:

<input class="email" name="email" value="{{ current_user.email }}" required/>

Upvotes: 0

JBLaf
JBLaf

Reputation: 828

You can overwrite request.form :

from werkzeug.datastructures import ImmutableMultiDict
from flask import Flask,request

app = Flask(__name__)

@app.route('/')
def hello_world():
    # overwrite request.form to add 'email'
    tempo_form_dict = request.form.to_dict()
    tempo_form_dict['email'] = 'Hello@World'
    request.form = ImmutableMultiDict(tempo_form_dict)
    
    ## Then you can use request.form as it was inputted...
    
    return str(request.form)

if __name__ == '__main__':
    app.run(debug=True,host=None,port=5000)

a linked question: Python Werkzeug: modify Request values (forms and args) prior to retrieval

Upvotes: 1

Related Questions