2

I have a toast for displaying errors.

$.toast({
    text: '{{ form.non_field_errors }}'
})

But it is returning a html format. so i am getting an error. enter image description here

But i would like to get text only from {{ form.non_field_errors }}. How do i do that?

1 Answer 1

1

You can use the .as_text() method, on the non_field_errors:

$.toast({
    text: '{{ form.non_field_errors.as_text }}'
})

Note however that in order to make this more safe, you better JSON-ify this, and furthermore mark the output as safe.

You thus better do some processing in the view:

from json import dumps as jdumps

def some_view(request):
    some_form = ModelForm(request.POST)
    errors = jdumps(some_form.non_field_errors().as_text())
    return render(request, 'some_template.html', {'errors': errors})

and in the template render this as:

$.toast({
    text: {{ errors|safe }}
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.