'django forms non_field_errors customizing
I want to have my own errors in django forms but I cant . as you can see I define my own error_messages but django is still using Its own errors.
app account / forms.py:
from django import forms
error_messages_email = {
'required': 'please Fill-out this field',
'invalid': 'invalid format for email',
'max_length': 'max length is 40 chars',
}
error_messages = {
'required': 'please Fill-out this field',
'invalid': 'fields format is not valid',
'max_length': 'max_length is 30 chars',
'min_length': 'password should be at least 8 Chars',
}
class LoginForm(forms.Form):
username = forms.CharField(error_messages=error_messages, max_length=30,
widget=forms.TextInput(attrs={'class': 'form-control',
'placeholder': 'Username'}))
password = forms.CharField(error_messages=error_messages, min_length=8, max_length=30,
widget=forms.PasswordInput(attrs={'class': 'form-control',
'placeholder': 'Password'}))
class SignupForm(forms.Form):
username = forms.CharField(error_messages=error_messages, max_length=30,
widget=forms.TextInput(attrs={'class': 'form-control',
'placeholder': 'Username'}))
email = forms.EmailField(error_messages=error_messages_email, max_length=40,
widget=forms.EmailInput(attrs={'class': 'form-control',
'placeholder': 'Email'}))
password = forms.CharField(error_messages=error_messages, min_length=8, max_length=30,
widget=forms.PasswordInput(attrs={'class': 'form-control',
'placeholder': 'Password'}))
and this is my apps template / signup.html:
{% extends 'main.html' %}
{% block title %} Join {% endblock %}
{% block content %}
<form action="" method="post">
{% csrf_token %}
{{ form.non_field_errors }}
{{ form.username.errors }}
<label for="{{ form.username.id_for_label }}">Username:</label>
{{ form.username }}<br>
{{ form.email.errors }}
<label for="{{ form.email.id_for_label }}">E-mail:</label>
{{ form.email }}<br>
{{ form.password.errors }}
<label for="{{ form.password.id_for_label }}">Password:</label>
{{ form.password }}<br>
<input type="submit" value="signup" class="btn btn-primary" >
</form>
{% endblock %}
the only problem is the errors other parts are working properly, in the browser I also change max_length to 50 and pass it a 50-char password and submit it and it works fine and shows my own error.
Solution 1:[1]
You can only use the error_messages keyword argument for a Model or a ModelForm.
As you're just using the standard Form this will not present the behaviour you're expecting.
You would need to change your LoginForm to inherit ModelForm. For example:
class LoginForm(forms.ModelForm):
username = forms.CharField(error_messages=error_messages, max_length=30,
widget=forms.TextInput(attrs={'class': 'form-control',
'placeholder': 'Username'}))
password = forms.CharField(error_messages=error_messages, min_length=8, max_length=30,
widget=forms.PasswordInput(attrs={'class': 'form-control',
'placeholder': 'Password'}))
class Meta:
model=User
fields = ('username','password')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
