S
S
Sergey2016-11-15 15:27:06
Django
Sergey, 2016-11-15 15:27:06

How to output errors to a template in Django?

I'm trying to display errors in the template after validation in Django, but I can't display it.
I do this:
In the view

# Создание пользователя
def admin_users_create(request):
    args = {}
    args.update(csrf(request))
    if request.POST:
        newuser_form = CustomUserCreationForm(request.POST)
        if newuser_form.is_valid():
            newuser_form.save()
            return redirect('/admin/users/')
        else:
            args['errors'] = newuser_form.errors.as_data()
    return render_to_response('admin/users_create.html', args)

in template
{% if errors %}
    <ol>
          {% for error in errors %}
              <li><strong>{{ error }}</strong></li>
          {% endfor %}
    </ol>
{% endif %}

but only dictionary keys are displayed, but I would like to see a list of errors.
At the same time, I'm not sure if I'm doing the right thing, that I get errors through as_data ().
Please, prompt how correctly to do an output of errors.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2016-11-15
@SereyK

Now I found what can be withdrawn

{% if errors %}
    {{ errors }}
{% endif %}

but in this case it is not clear how to display errors with custom layout

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question