0

I am making a login page for my Django app. At the moment, if I enter the wrong username or password the following error message shows up:

error.

Is there a way that I could customise this message to get rid of the "___all____" and the bullet points? Perhaps just saying "Please enter a correct username and password. Note that both fields may be case-sensitive."?

A simplified version of my code is below:

views.py:

from django.contrib.auth import authenticate,login,logout

html:

<div class="form_container">
    <form class="log_in_form" action="{% url 'inventory_management_app:login' %}" method="post">
    <p>
    {% csrf_token %}

    {{ form.username }}
    </p>

    <br><br>

    <p>
    {{ form.password }}

    <br>

    {{ form.errors }}
    </p>
    <p>

    <br><br>

  <input id="search-button" class="btn btn-dark" type="submit" value="Login">
  </form>
  </p>
  </div>

forms.py:

from django import forms
from django.contrib.auth.forms import AuthenticationForm
from django.forms.widgets import PasswordInput, TextInput


class CustomAuthForm(AuthenticationForm):
    username = forms.CharField(widget=TextInput(attrs={'class':'validate','placeholder': 'Email'}))
    password = forms.CharField(widget=PasswordInput(attrs={'placeholder':'Password'}))

urls.py

url(r'^login/$', auth_views.login, name='login', kwargs={"authentication_form":CustomAuthForm}),

EDIT: Following marxin's answer allowed me to create a custom message. However, my message was still displaying as a bullet point, a screenshot is below: enter image description here

3 Answers 3

7

Change invalid_login error message in your form class.

class CustomAuthForm(AuthenticationForm):
    def __init__(self, *args, **kwargs):
        self.error_messages['invalid_login'] = 'Custom error'
        super().__init__(*args, **kwargs)
Sign up to request clarification or add additional context in comments.

5 Comments

This works great, thank you! The only problem is, my error message is still appearing in bullet points. I have to change the display of {{ form.errors }} in my HTML to show the message without bullet points. Do you know how I could do this Marxin?
{{ form.errors }} is most likely a dictionary so you should be able to iterate over it in template. {% for key, msg in form.errors.items %} {{ key }} {{ msg }} {% endfor %}
Ah! This removes the first bullet point but it still displays the sub message "custom error" as a bullet point
I'm adding in the new screenshot to my question
I found an alternative answer that is simpler to implement and does not result in bullet points and dictionary for loops so I am not going to accept your answer. I have voted it up as an alternative answer for someone to try if it suits their specific needs..
1

After implementing marxins answer, I was still getting difficulties removing the bullet points from my custom message. I found that by just changing my template to include the following code, I could customise my error message without altering anything in my forms.py. The rest of my code is identical to the code given in the question but I have added this:

{% if form.errors %}
    <p>my custom error message</p>
{% endif %}

So now my HTML looks like this:

<form class="log_in_form" action="{% url 'inventory_management_app:login' %}" method="post">
  <p>

  {% csrf_token %}

  {{ form.username }}
  </p>

  <br>

  <p>
  {{ form.password }}
  <br>
  <p>
  </p>
  <br>

  {% if form.errors %}
      <p>username or password not correct</p>
  {% endif %}

  </p>
  <p>
  <br><br>
  <input id="search-button" class="btn btn-dark" type="submit" value="Login">
  </form>

1 Comment

But in this code , you will have the same error message in the case of incorrect login user is inactive
0

You can use form validation to introduce a custom method. please refer to https://docs.djangoproject.com/en/2.1/ref/forms/validation/#cleaning-and-validating-fields-that-depend-on-each-other

2 Comments

How would I specifically implement this for a login form?
using a clean method on your form class.

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.