Django:表单创建时的表单验证错误

时间:2019-02-03 20:51:06

标签: python django validation django-forms

假设我有一个带有ChoiceField的Django表单:

    <div class="margin">
    <div class="row">
        <div class="col">
            <h2>Suchkriterien</h2>
        </div>
        <img src="assets/images/Gruppe.jpg" height="70" width="408" alt="Logo" align="right">

  </div>
    <form target="Rueckgabe" method="post" action="Lieferantenkarte.php">
        <div class="row">
            <div class="col-sm">
                <div class="form-group">
                    <label for="Handwerkwaehlen">Handwerk</label>
                    <div class="ui-widget">
                        <input id="gewerke" class="form-control txt-auto"/>
                    </div>
                </div>
            </div>
            <div class="col-sm">
                <div class="form-group">
                    <label for="Standortwaehlen">Standort</label>
                    <input type="text" class="form-control" id="Standortwaehlen" name="Standort" placeholder="Standort eingeben" onFocus="geolocate()">
                </div>
            </div>
            <div class="col-sm">

            </div>
        </div>
        <div class="row">
            <div class="col-sm">
                <div class="form-group">
                    <label for="Google Suche">Google suche aktivieren?</label>
                    <input type="checkbox" class="form-control" id="checkbox" name="checkbox" >

                </div>
            </div>
            <div class="col-sm">
                <label class="form-check-label" for="Radius">Suchradius</label>
                <input type="range" id="Radius" name="radius" class="form-control" min="2" max="100" value="5">
                <span id="Radius_Wert"></span><a> km Radius</a>
            </div>
            <div class="col-sm">

            </div>
        </div>
        <div class="row">
            <div class="col-sm">
                <button  type="submit" name="submit" id="button" class="btn btn-primary">Suchen</button>
            </div>
        </div>
    </form>
    <div class="row">
        <div class="col-md">
            <iframe  width="1000px" height="500px" name="Rueckgabe">

            </iframe>
        </div>
        <div class="col-md">
            <div id="map"></div>
            <div id="legend"><h3>Legend</h3></div>

        </div>

    </div>

Choisefield正在初始化以“空选择”为首的对象列表。没有对象的pk = 0。 在这种形式下,我有一个clean()方法:

class MyForm(forms.Form):
    name = forms.CharField(label="Name", required=True)
    some_object = forms.ChoiceField(label="Object", choices=[(0, '-----')] + [(x.id, x.name) for x in Obj.objects.all()])

当我向服务器发送表单时,如果它的数据不符合条件,则它会很好地工作,field.is_valid返回False,并且我用错误消息呈现表单。但是,当我创建这样的空表单时:

def clean(self):
    if not Obj.objects.filter(self.cleaned.data['some_object'].exists():
        self.errors.update({'some_object': ['Invalid choice']})

Django会使用错误消息(“无效选择”)呈现表单,即使表单刚刚创建且“ Object”选择旨在设置为空位置。在特定情况下是否可以禁用form clean()方法?还是我做错了所有?创建空表格的最佳实践是什么?

1 个答案:

答案 0 :(得分:1)

问题在于,带有任何数据字典的表单都将被视为“绑定”表单,Django将针对该表单执行数据验证。有关详情,请参见此处:https://docs.djangoproject.com/en/2.1/ref/forms/api/#bound-and-unbound-forms

您希望“未绑定”表单-在此处具有默认初始值,只需在表单字段上设置initial属性即可。在此处查看完整的详细信息:https://docs.djangoproject.com/en/2.1/ref/forms/fields/#initial