mercredi 13 août 2014

python - Django. Form.Errors n'est pas visible dans le modèle, même si le formulaire est lié - Stack Overflow


I'm submitting a form with invalid data: string that contain's less then 5 characters (see forms.py), I see that the form is bound, I print form.errors from the view to see the actual errors, I pass form to the template but form.errors is empty in template!


views.py


def index(request):
if request.method == 'POST':
form = OptionForm(request.POST)
if form.is_valid():
print "VALID"
return HttpResponseRedirect(reverse('main:success'))

elif request.method == 'GET':
form = OptionForm()
print form.is_bound # --> True
print form.errors # --> errors, see below
return render(request, 'main/index.html', {'form': OptionForm})

printed form.errors contains:


<ul class="errorlist"><li>opt<ul class="errorlist"><li>Ensure this value has at least 5 characters (it has 3).</li></ul></li></ul>

forms.py


class OptionForm(forms.Form):
opt = forms.CharField(min_length=5)

index.html


<!-- form errors -->
{{ form.errors }}
<form id="option-set" method="post">
{% csrf_token %}

{{ form.opt }}

<input type="submit" value="submit">
</form>

However, if I pass form.error salong with the form object, I will be able to see them.


return render(request, 'main/index.html', {'form': OptionForm, 'errors': form.errors})

As I know, errors, or other attributes, should be accessible directly in the template.


I can't see where is the bug! :)




You're passing the form class, OptionForm, rather than the instantiated variable form, to the template.



I'm submitting a form with invalid data: string that contain's less then 5 characters (see forms.py), I see that the form is bound, I print form.errors from the view to see the actual errors, I pass form to the template but form.errors is empty in template!


views.py


def index(request):
if request.method == 'POST':
form = OptionForm(request.POST)
if form.is_valid():
print "VALID"
return HttpResponseRedirect(reverse('main:success'))

elif request.method == 'GET':
form = OptionForm()
print form.is_bound # --> True
print form.errors # --> errors, see below
return render(request, 'main/index.html', {'form': OptionForm})

printed form.errors contains:


<ul class="errorlist"><li>opt<ul class="errorlist"><li>Ensure this value has at least 5 characters (it has 3).</li></ul></li></ul>

forms.py


class OptionForm(forms.Form):
opt = forms.CharField(min_length=5)

index.html


<!-- form errors -->
{{ form.errors }}
<form id="option-set" method="post">
{% csrf_token %}

{{ form.opt }}

<input type="submit" value="submit">
</form>

However, if I pass form.error salong with the form object, I will be able to see them.


return render(request, 'main/index.html', {'form': OptionForm, 'errors': form.errors})

As I know, errors, or other attributes, should be accessible directly in the template.


I can't see where is the bug! :)



You're passing the form class, OptionForm, rather than the instantiated variable form, to the template.


0 commentaires:

Enregistrer un commentaire