PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, July 10, 2022

[FIXED] How to remove username and password2 fields from form errors

 July 10, 2022     django, message     No comments   

Issue

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)


Solution

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 class="messages">
    {% for message in messages %}
      <li {% if message.tags %} class="{{ message.tags }}"{% endif %}>
        {{ message|safe }}
          <button  class="close">x</button>
      </li>
    {% endfor %}
    </ul>
  {% endif %}


Answered By - eisa nahardani
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing