Acla
Acla

Reputation: 141

Django - remove default localized messages (not error or help messages)

I'm creating a contact form using Django and I can't seem to figure out where this message (check image bellow) is coming from. It's a localized (in portuguese) message saying 'Please enter an email address'. It's not the field error message (I've customized it and it appears below the field, in english). I thought it was field.help_text but I was able to change that as well and this foreign message still shows up.

Does anyone know how I can override and style this type of messages? Thanks in advance!

enter image description here

<form action="" method="POST" class="form">  {% csrf_token %}
        {% for field in form %}
        <div class="fields-container">
            <label class="label">{{ field.label }}</label>
            {{ field }}
        </div>
        <p class="server-form-errors"> {{ field.errors.as_text }} </p>
        {% endfor %}
        <button type="submit" id="form-button">Submit</button>
    </form>

forms.py:

from django import forms
from django.core.validators import RegexValidator   # import!

class ContactForm(forms.Form):
    full_name = forms.CharField(required=True)
    email = forms.EmailField(validators=[RegexValidator(regex=r'^[a-zA-Z\d.+*?=_@^$%&#´\{\}\|~-]+@([a-zA-Z0-9@-_]+)(\.[a-zA-Z0-9@-_]+)+\b')]
                            , error_messages={'invalid': 'Please enter a valid email address.'})
    message = forms.CharField(widget=forms.Textarea)

Upvotes: 0

Views: 413

Answers (1)

Alasdair
Alasdair

Reputation: 309039

The error message is coming from your browser (e.g. Chrome), not from Django. It is validating the field because it has type="email". I assume that the error is in Portuguese because your browser is set to Portuguese.

You can prevent the browser from validating the form by setting the novalidate attribute.

<form method="post" novalidate>
    <input type="email" />
    <input type="submit" />
</form>

Upvotes: 2

Related Questions