Issues trying to show form errors on Django

Skip to first unread message

Fellipe Henrique

Mar 15, 2018, 11:49:37 AM3/15/18
to pocoo-libs
I start moving from django template to jinja2... but I have a issue when try to display a form errors

I have these:

class AccountLogin(View):
template_name = 'account/login.html'

def get(self, request, *args, **kwargs):
return render(request, self.template_name, {'form': LoginForm()})

def authenticate_user(self, form):
username = form.cleaned_data['username']
password = form.cleaned_data['password']
return authenticate(username=username, password=password)

def post(self, request, *args, **kwargs):
form = LoginForm(request.POST)
if form.is_valid():
user = self.authenticate_user(form)
if user is not None:
if user.is_active:
login(request, user)
return redirect(settings.LOGIN_REDIRECT_URL)
error_msg = _("Usuário Inativo. Por favor, entre em contato com um Administrador.")

error_msg = _("Usuário/Senha incorretos. Por favor, tente novamente.")
form.add_error(None, error_msg)
form.add_error(None, 'ERRRR?OR?ORERERE')

return render(request, self.template_name, {'form': form})

and these

        {%  with form=form %}
{% include "base/_form_error_msgs.html" %}

{% endwith %}

and these:

{% if form.errors %}
{% for field in form %}
{% for error in field.errors %}
<div class="alert alert-danger">
<strong>{{ error|escape }}</strong>
{% endfor %}
{% endfor %}
{{ form.errors }}
{% endif %}

As result I have these:

  • Usuário/Senha incorretos. Por favor, tente novamente.

These result came from {{ form.errors }}  not for field.errors  but I can't show each error separate...  on django template I just need to iterate over form.fields and form.non_field_errors

How can I how with that on Jinja2?


Reply all
Reply to author
0 new messages