简体   繁体   English

Request.form 找不到我的字段,即使它们似乎存在

[英]Request.form not finding my fields even though they seem to exist

I am trying to submit a form with Flask using dynamic field names for multiple check boxes within a form.我正在尝试使用 Flask 提交表单,该表单使用表单中多个复选框的动态字段名称。 The naming of the fields seems to work and they have the names I want, but when I try to use request.form.get() to get the information from the form using the dynamic field names it is just returning None, meaning that the dictionary that is returned does not contain the names of my fields.字段的命名似乎有效并且它们具有我想要的名称,但是当我尝试使用 request.form.get() 使用动态字段名称从表单中获取信息时,它只是返回 None,这意味着返回的字典不包含我的字段名称。 Here is my code:这是我的代码:

The Form And Example of name名称的形式和例子

![形式

示例名称

HTML HTML

<form action="{{url_for('main.updatealleles', id=fish.id)}}" method="post">
    <div id="updateAllelesBlock">
        <table id="alleleTable" style="margin: auto; margin-top:20px;">
            <tr>
                <th>Name</th>
                <th>Unidentified</th>
                <th>Identified</th>
                <th>Heterozygous</th>
                <th>Homozygous</th>
                <th>Hemizygous</th>
            </tr>
            {%for allele in alleles%}
                <tr>
                    <th>{{allele.name}}</th>
                    <td><input type="checkbox" name = {{allele.name.replace(" ", "")+"Unidentified"}}</td>
                    <td><input type="checkbox" name = {{allele.name.replace(" ", "")+"Identified"}}</td>
                    <td><input type="checkbox" name = {{allele.name.replace(" ", "")+"Heterozygous"}}</td>
                    <td><input type="checkbox" name = {{allele.name.replace(" ", "")+"Homozygous"}}</td>
                    <td><input type="checkbox" name = {{allele.name.replace(" ", "")+"Hemizygous"}}</td>
                </tr>
            {%endfor%}
        </table>
    </div>
    <button type="submit" class="btn btn-secondary" style="margin-top: 10px;">Update</button>
</form>

PYTHON PYTHON

@bp.route("/fish/<id>/updatealleles/", methods=["GET", "POST"])
@login_required
def updatealleles(id):
    fish = Fish.query.filter_by(id=id).first_or_404()
    alleles = Allele.query.filter_by(fish=fish).all()

    if request.method == "POST":
        for allele in alleles:
            print(request.form.get(f"{allele.name.replace(' ', '')}Unidentfied"), file=sys.stderr)
            print(request.form.get(f"{allele.name.replace(' ', '')}Identfied"), file=sys.stderr)
            #THIS IS ALWAYS PRINTING NONE, SAME WITH ALL OTHER FIELDS

            #therefore these, and the other fields are being set to None rather than true/false
            allele.unidentified = request.form.get(f"{allele.name.replace(' ', '')}Unidentified")
            allele.identified = request.form.get(f"{allele.name.replace(' ', '')}Identified")

        db.session.commit()

            return redirect(url_for("main.updatealleles", id=fish.id))
    
        

    return render_template('updatealleles.html', fish=fish, alleles = alleles, title = "Update Alleles")

The reason I need the dynamic name fields is because there could be any number of different allele objects (they are SQLAlchemy objects) and the table is there to set different attributes for each allele object to true or false, shown in the python example.我需要动态名称字段的原因是因为可能有任意数量的不同等位基因对象(它们是 SQLAlchemy 对象),并且该表用于将每个等位基因对象的不同属性设置为真或假,如 python 示例所示。 If there is a completely different way to do this that I am no aware of then I am also happy to try that.如果有一种我不知道的完全不同的方法来做到这一点,那么我也很乐意尝试。

I know there have been some similar questions to this, but I have tried all of their suggestions and none of them have worked for me, so any help would be greatly appreciated and let me know if you need any more information.我知道有一些类似的问题,但我已经尝试了他们的所有建议,但没有一个对我有用,所以任何帮助将不胜感激,如果您需要更多信息,请告诉我。

Okay I have the solution.好的,我有解决方案。 It turns out even though the names appeared to be working when inspected, the jinja2 string was actually returned as unclosed when the form was requested extra information was included in the name.事实证明,即使名称在检查时似乎有效,但当请求表单时,jinja2 字符串实际上返回为未封闭的,名称中包含额外信息。 All I had to do to fix that was to add single quotation marks, either side of the id, so now it looks like this, for example:我所要做的就是在 id 的任一侧添加单引号,所以现在它看起来像这样,例如:

 <td><input type="checkbox" name = '{{allele.name.replace(" ", "")+"Unidentified"}}'</td>

and then the python can work like this:然后python可以像这样工作:

allele.unidentified= request.form.get(f"{allele.name.replace(' ', '')}Unidentified") == "on"

And the same for each other field.对于其他领域也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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