简体   繁体   中英

How to sed data from database to html form in Flask for editing

I am trying to send the data from the database to the Html form, but I'm getting an error like this

TypeError: 'ImmutableMultiDict' objects are immutable

this is the flask code for editing the data

@app.route('/edit-project/<string:id>', methods = ['GET', 'POST'])
def edit_project(id):

    project = Project.query.get(id)

    request.form['title'] = project.title
    request.form['link'] = project.link
    request.form['description'] = project.description

    if request.method == 'POST':

        title = request.form['title']
        link = request.form['link']
        description = request.form['description']
        image = request.files['image']

        image.save(os.path.join(app.config["IMAGE_UPLOADS"], 'project/'+ project.image))


    return render_template('project/edit_project.html')

The Html template 'edit_project.html' is shown below

<div class="container">
    <form method="POST" id="project-form" enctype="multipart/form-data">
        <div class="form-group">
            <input type="text" name="title" class="form-control" id="title" placeholder="Title">
        </div>
        <div class="form-group">
            <input type="text" name="link" class="form-control" id="title" placeholder="Project Link">
        </div>
        <div class="form-group">
            <label for="exampleFormControlTextarea1">Description</label>
            <textarea id = project-form name="description" class="form-control" id="description" rows="3"></textarea>
        </div>
        <div class="input-group" style="width: 30%;">
            <div class="input-group-prepend">
                <span class="input-group-text" id="inputGroupFileAddon01">Upload</span>
            </div>
            <div class="custom-file">
                <input type="file" name="image" class="custom-file-input" id="inputGroupFile01" aria-describedby="inputGroupFileAddon01">
                <label class="custom-file-label" for="inputGroupFile01">Upload image</label>
            </div>
        </div>
        <div class="form-group" style="padding-top: 2rem;">
            <button type="submit" class="btn btn-primary">Publish</button>
        </div>
    </form>
</div>

Is there is any way to display the data in the form without using WTForms or do I've to use WTForms instead?

Thanks in advance.

Can you use jinja templating? render_template() takes data argument. You can pass the data from your DB to that call and use jinja templates in HTML to render it. Setting the values of your input will do. For example: Your data is data = {title:"apple"} And, return render_template("project/edit_project.html", data=data) will provide you data object in HTML file. There, you can use jinja like this:

<div class="form-group">
    <input type="text" name="title" class="form-control" id="title" placeholder="Title" value={{data.text}}>
</div>

Hope it helps!

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