简体   繁体   中英

Django accepts an integer into a CharField

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??

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM