Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
83 views
in Technique[技术] by (71.8m points)

python - Not able to retrieve hidden fields from my html

Currently I have a table being constructed into my HTML reading in a for loop (jinja2) to fill out the data from backend. There is one hidden field that Im not able to retrieve the value back once button save_quality is clicked. The others form fields as dimension and schedule are being retrieved successfully, just the hidden one is the problem. Any clue?

HTML:

    <table id="qualityTable" class="table table-striped table-sm table-borderless">
        <thead class="grey">
            <tr>
                <th class="th-sm white-text" scope="col"><i class="fas fa-id-card white-text"> <strong>Data Asset - Quality Plan</strong></th>
                <th class="th-sm white-text" scope="col"><i class="fas fa-cubes white-text"> <strong>Dimension</strong></th>
                <th class="th-sm white-text" scope="col"><i class="fas fa-calendar white-text"> <strong>Schedule</strong></th>
            </tr>
        </thead>
        <tbody>
            {% for asset in asset_list %}
                <form method="POST">
                    {{ csrf_token }}
                    <input type="hidden" id="{{ asset.id }}" name="asset_id" value="{{ asset.id }}">
                    <tr>
                        <td>
                            <div class="card w-50">
                                <h6 class="card-header {{ color }} white-text"><i class="fas fa-database"> {{ asset.label|string|upper }}</i></h6>
                                    {% if asset.quality %}
                                        <div class="card-body">
                                            {% for quality in asset.quality %}
                                                <p class="card-text"><a href="#"><i class="fas fa-1x fa-trash-alt red-text"></i></a> Dimension <strong>{{ quality.dimension|upper }}</strong> is scheduled to run <strong>{{ quality.schedule }}</strong>.</p>
                                            {% endfor %}
                                        </div>
                                    {% else %}
                                    <div class="card-body">
                                        <p class="card-text">No quality assessment plan has been created.</p>
                                    </div>
                                    {% endif %}
                            </div>
                            <hr>
                            <button type="submit" id="clean" name="clean" value="clean" class="btn btn-outline-red btn-rounded white-text btn-sm"><i class="fas fa-trash"></i>Clean up</button>
                            <a role="button" href="{{ url_for('overview.profile', asset_id = asset.id) }}" class="btn btn-outline-{{ color }} btn-rounded btn-sm"><i class="fas fa-id-card"></i> Profiling</a>
                        </td>
                        <td>
                            <select class="browser-default custom-select" id="dimension_{{ asset.name }}" name="dimension">
                                <option value="" disabled selected>Dimension...</option>
                                    <option value='accuracy'>Accuracy</option>
                                    <option value='completeness'>Completeness</option>
                                    <option value='consistency' disabled>Consistency</option>
                                    <option value='update'>Update</option>
                                    <option value='precision' disabled>Precision</option>
                                    <option value='accordance' disabled>Accordance</option>
                                    <option value='integrity' disabled>Integrity</option>
                                    <option value='unicity'>Unicity</option>
                                    <option value='validity' disabled>Validity</option>
                            </select>
                            <br>
                            <button type="submit" id="ondemand" name="ondemand" value="ondemand" class="btn btn-outline-primary btn-rounded white-text btn-sm"><i class="fas fa-play"></i></button>
                            {% if asset.id == selected_asset_id %}
                                <br>
                                {% if 'completeness' in dimension or 'accuracy' in dimension or 'unicity' in dimension %}
                                    <h6 class="{{ color }}-text">{{ dimension|capitalize }}: <strong>{{ result }} %</strong></h6>
                                {% else %}
                                    <h6 class="{{ color }}-text">{{ dimension|capitalize }}: <strong>{{ result }}</strong></h6>
                                {% endif %}
                            {% endif %}
                            <a role="button" data-toggle="popover" title="Information" data-content="Each dimension looks differently to the data set. To select the right dimension be sure it is important for business rules regarding this data asset. Select dimensions you want to add to quality engine to be monitored automatically."><i class="fas fa-info {{ color }}-text fa-1x"></i></a>
                        </td>
                        <td>
                            <select class="browser-default custom-select" id="schedule_{{ asset.name }}" name="schedule">
                                <option value="" disabled selected>Schedule...</option>
                                    <option value='daily'>Daily</option>
                                    <option value='monthly'>Monthly</option>
                                    <option value='yearly'>Yearly</option>
                            </select>
                            <br>
                            <button type="submit" id="save_quality" name="save_quality" value="save_quality" class="btn btn-outline-primary btn-rounded white-text btn-sm"><i class="fas fa-plus"></i></button>
                            <a role="button" data-toggle="popover" title="Information" data-content="The chosen schedule will be the agenda for quality engine to run. A best practice is to run quality measure monthly."><i class="fas fa-info {{ color }}-text fa-1x"></i></a>
                        </td>
                    </tr>
                </form>
            {% endfor %}
        </tbody>
        <tfoot class="grey">
            <tr>
                <th class="th-sm white-text" scope="col"><strong>Data Asset - Quality Plan</strong></th>
                <th class="th-sm white-text" scope="col"><strong>Dimension</strong></th>
                <th class="th-sm white-text" scope="col"><strong>Schedule</strong></th>
            </tr>
        </tfoot>
    </table>

The line with hidden field:

<input type="hidden" id="{{ asset.id }}" name="asset_id" value="{{ asset.id }}">

My backend getting an action from button and taking other form fields, like dimension and schedule (that is working fine):

if 'save_quality' in request.form:
    asset_id = request.form.get('asset_id')
    dimension = request.form.get('dimension')
    schedule = request.form.get('schedule')
    #DB processes
    found_asset = Asset.query.filter_by(id=asset_id).first()
    last_run = None
    next_run = None
    if found_asset and found_asset.quality:   #asset found and already have quality data registered for that asset
        quality = Quality.query.filter_by(asset_id=found_asset.id).all()
        for item in quality:
            if item.dimension == dimension:
                rows_changed = Quality.query.filter_by(id=item.id).update(dict(dimension=dimension,schedule=schedule,last_run=last_run,next_run=next_run,asset_id=found_asset.id))
                db.session.commit()
            else:
                new_quality = Quality(dimension=dimension,schedule=schedule,last_run=last_run,next_run=next_run,asset_id=found_asset.id)
                db.session.add(new_quality)
                db.session.commit()
    else:
        new_quality = Quality(dimension=dimension,schedule=schedule,last_run=last_run,next_run=next_run,asset_id=found_asset.id)
        db.session.add(new_quality)
        db.session.commit()
question from:https://stackoverflow.com/questions/65929481/not-able-to-retrieve-hidden-fields-from-my-html

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...