表单的会话值没有显示验证错误,例如
{{object}}没有显示
形式class EmpresaForm(forms.ModelForm):
class Meta:
model = models.Empresa
fields = [
"fantasia",
"razao",
"cnpj",
"seguimento"
]
视图
class EmpresaCreateView(generic.CreateView):
model = models.Empresa
form_class = forms.EmpresaForm
模板
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
{{form.errors}}
<div class="form-group row">
<label class="col-sm-2 col-form-label" for="fantasia">fantasia: </label>
<input class="form-control col-sm-10" id="fantasia" type="text" name="fantasia" value="{{ object.fantasia }}">
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label" for="razao">razao: </label>
<input class="form-control col-sm-10" id="razao" type="text" name="razao" value="{{ object.razao }}">
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label" for="cnpj">cnpj: </label>
<input class="form-control col-sm-10" id="cnpj" type="text" name="cnpj" value="{{ object.cnpj }}">
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label" for="seguimento">seguimento: </label>
<input class="form-control col-sm-10" id="seguimento" type="text" name="seguimento" value="{{ object.seguimento }}">
</div>
<input type="submit" value="Save" class="btn btn-primary">
</form>
检查模型字段的正确拼写。可能是拼写错误。
修改代码:
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
{{form.errors}}
<div class="form-group row">
<label class="col-sm-2 col-form-label" for="fantasia">fantasia: </label>
<input class="form-control col-sm-10" id="fantasia" type="text" name="fantasia" value="{{ object.fantasia }}">
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label" for="razao">razao: </label>
<input class="form-control col-sm-10" id="razao" type="text" name="razao" value="{{ object.razao }}">
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label" for="cnpj">cnpj: </label>
<input class="form-control col-sm-10" id="cnpj" type="text" name="cnpj" value="{{ object.cnpj }}">
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label" for="seguimento">seguimento: </label>
<input class="form-control col-sm-10" id="seguimento" type="text" name="seguimento" value="{{ object.seguimento }}">
</div>
<input type="submit" value="Save" class="btn btn-primary">
</form>
:
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
{{form.errors}}
<div class="form-group row">
<label class="col-sm-2 col-form-label" for="fantasia">fantasia: </label>
<input class="form-control col-sm-10" id="fantasia" type="text" name="fantasia" value="{{ form.fantasia }}">
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label" for="razao">razao: </label>
<input class="form-control col-sm-10" id="razao" type="text" name="razao" value="{{ form.razao }}">
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label" for="cnpj">cnpj: </label>
<input class="form-control col-sm-10" id="cnpj" type="text" name="cnpj" value="{{ form.cnpj }}">
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label" for="seguimento">seguimento: </label>
<input class="form-control col-sm-10" id="seguimento" type="text" name="seguimento" value="{{ form.seguimento }}">
</div>
<input type="submit" value="Save" class="btn btn-primary">
</form>