python - Django - error message does not show on the form when submited -
hi have issue django form validation, here problem :
when submit form , validation not respecting condition, validationerror() not raise message.
here code :
views.py
def addarticle(request): if request.method == 'post': form = articleform(request.post, request.files) if form.is_valid(): save_it = form.save(commit=false) save_it.user = request.user save_it.save() form.save_m2m() return httpresponseredirect('/') else: form = articleform() context = {'form':form} return render(request, 'template.html', context, context_instance=requestcontext(request))
template.html
<form class="form" action="{% url "article.views.addarticle" %}" method="post" enctype='multipart/form-data'> {% csrf_token %} {{ form.non_field_errors }} <div class="form-group fieldwrapper"> {{ form.titre.errors }} {{ form.titre.label_tag }} {{ form.titre }} </div> <div class="form-group fieldwrapper"> {{ form.image.errors }} {{ form.image }} </div> <input type="submit" class="btn btn-default" value="submit" autocomplete="off" autocorrect="off" /> </form>
forms.py
class articleform(forms.modelform): class meta: model = article def clean_titre(self): titre = self.cleaned_data['titre'] if len(titre) < 5: raise validationerror('titre error') return titre form = articleform()
what doing wrong ? why error message doesn't display on website?
{{ form.titre.error }}
the line above should be:
{{ form.titre.errors }}
it shown way in django documentation
Comments
Post a Comment