42๐
โ
How about adding the error code:
user = CharField(
max_length=30,
required=True,
validators=[
RegexValidator(
regex='^[a-zA-Z0-9]*$',
message='Username must be Alphanumeric',
code='invalid_username'
),
]
)
๐คHieu Nguyen
8๐
I was having trouble running a RegexValidator, too. But I was trying to raise the error by saving the model instance. It will not work this way! Only when using ModelForms the validators are called automatically.
In https://docs.djangoproject.com/en/dev/ref/validators/#how-validators-are-run
Note that validators will not be run automatically when you save a model, but if you are using a ModelForm, it will run your validators on any fields that are included in your form.โ
๐คjrvidotti
- [Django]-Signal only works in main thread
- [Django]-Generic many-to-many relationships
- [Django]-Get objects from a many to many field
0๐
Try passing the messsage
as,
user = CharField(
max_length=30,
required=True,
validators=[
RegexValidator(
regex=r'^[a-zA-Z0-9]*$',
message=_('Username must be Alphanumeric'),
),
]
)
๐คViren Rajput
- [Django]-"settings.DATABASES is improperly configured" error performing syncdb with django 1.4
- [Django]-Model.objects.get() or None
- [Django]-Why does django's prefetch_related() only work with all() and not filter()?
0๐
a validate user name here should contain at least one minuscule letter, one capital letter and one numeric, if i understand your code.
to complete Virendra Rajput answer correct the regex with that:
regex=r'^[a-zA-Z0-9]*$' start with the r'
๐คdrabo2005
- [Django]-How can one use enums as a choice field in a Django model?
- [Django]-How to remove models from django?
- [Django]-What is the way to ignore/skip some issues from python bandit security issues report?
Source:stackexchange.com