Home > OS >  How to remove username and password2 fields from form errors
How to remove username and password2 fields from form errors

Time:02-22

I have a problem with these fields in their output to the html template. I need to output only error text without field data in the Messages function. error on website

Create messages in views.py messages.error(request, form.errors)

CodePudding user response:

from django.utils.safestring import mark_safe
from django.contrib import messages

def your_view(request):
    ...
    if form.is_valid():
        do_something
    else:
       error_text = '<ul style="display:flex;'\
              'flex-direction:column;list-style-type:none;"'
       for msg_list in form.errors.values():
           for msg in msg_list:
               error_text  = f'<li>{msg}</li>'
       error_text  = '</ul>'
    messages.error(request, mark_safe(error_text))
    # write your other code

Also use safe filter in your template:

    {% if messages %}
    <ul >
    {% for message in messages %}
      <li {% if message.tags %} {% endif %}>
        {{ message|safe }}
          <button  >x</button>
      </li>
    {% endfor %}
    </ul>
  {% endif %}
  • Related