Reputation: 423
I am learning django and created a Page form class
like so:
from django import forms
class Page(forms.Form):
title = forms.CharField(max_length = 200, min_length = 10)
body = forms.CharField(min_length = 200, max_length = 8000)
author = forms.CharField(min_length = 5, max_length = 60)
price = forms.FloatField(min_value = 10, max_value = 50)
I fiddled with it in the interactive shell and found out that when I enter an integer
, it simply accepts and validates all the fields although it did convert the integer
to string
first.
Isn't it plainly problematic? I mean I would not want any digit or any symbol in the Author field
. Do I need to write my additional clean_author method
or do I have any other django Form class
at my disposal to address exactly this issue??
Upvotes: 4
Views: 1651
Reputation: 351
There is no field type that will validate that kind of data.
You can easily validate the data enter by users using regular Expressions on backend or the frontend.
exp = '[\w\s]+'
This will only accept words and whitespaces which is perfect for author name
https://regexr.com/ use this site to make your regular expression.
Upvotes: 1