[Oldboy-django] [2 in-depth Django] generate form labels and set properties for labels

Source: Internet
Author: User

#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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.