#form generates HTML tagsA. Create input input box through form, form label, and submit tag to write on the front end, but the input tag inside the form tag can be implemented in the background; just follow the steps below-views definition Studentform (Form) class-The Views view function passes the form instantiation object to the front end-front End {{obj. segment}} b. Set the Type property of the front-end input via form, that is, set different types of input boxes#set name to text, cls_id as drop-down box classStudentform (Form): Name= fields. Charfield (widget=widgets. Inputtext ()) cls_id= fields. Integerfield (widget =widgets. Select ()) c. Set the contents of the drop-down box choices PropertiesclassStudentform (Form): cls_id=Fields . Integerfield (widget=widgets. Select (choices=models. Classes.objects.values_list ('ID','title')) Note: The value of choices must be a [tuple, (), ()] Type widget=widgets. Select (choices=[(1,'Shanghai'), (2,'Beijing')] D. Set the class property of the input box--attrs name= fields. Charfield (Max_length=8, min_length=2, Widgets=widgets. TextInput (attrs={'class':'Form-control'})) cls_id=Fields . Integerfield (widget=widgets. Select (Choices=models. Classes.objects.values_list ('ID','title'), Attrs={'class':'Form-control'}) Note: The Attrs parameter must be placed inside the TextInput or select, and the value must be Dictionary E. Set by Form Default display value for front-end input as long as the view function instantiates a form object and sets the initial value to Student_dict= Models. Student.objects.filter (Id=nid). VALUES ('name',' Age','Email','cls_id'). First () obj= Studentform (initial=student_dict)
[Oldboy-django] [2 in-depth Django] generate form labels and set properties for labels