Django study notes---form component validation, how to add styles to form components

views.py

from django import forms
from django.forms import widgets
from django.forms import fields
class FM(forms.Form):
    # Because fields such as charField can only be verified, the default input box is used. If you want to use other html, you need to use the widgets plugin to add styles. [Why, don't know])
    user = fields.CharField(error_messages={'required':'Username cannot be empty'}
                           ,widget=widgets.Textarea(attrs={'class':'c1'}) # attr can customize the style c1
                           ) # custom error message
    pwd = fields.CharField(max_length=12,min_length=6,
                          error_messages={'required':"password cannot be empty","max_length":"password length is less than 12","min_length":"password length is greater than 6"},
                          widget=widgets.PasswordInput(attrs={'class':'c2'})
                          )
    email = fields.EmailField(error_messages={'required':'The email address cannot be empty','invalid':"The email format is incorrect"})

process:





So if you write obj.user in fm.html, the input box will be generated by default. If you need to change it, you need to use the widgets plugin

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325570642&siteId=291194637