[Oldboy-django] [2 in-depth Django] teacher Management--view, add, edit

Source: Internet
Author: User

#Add teacher (dropdown box multiple selection)Database design:classTeacher (models. Model): Name= Models. Charfield (max_length=64) CLS= Models. Manytomanyfield ('Classes')                    -form Set class input box for select multiple selection-Multi-SelectclassTeacherform (Form): Name= fields. Charfield (max_length=16, Widgets=widgets. TextInput (attrs={'class':'Form-control'})) CLS=Fields . Multiplechoicefield (Choices=models. Classes.objects.values_list ('ID','title'), Widgets=widgets. Selectmultiple (attrs={'class':'Form-control'})                )                        -PS single choice, that is, a teacher can only choose one classclassTeacherform (Form): Name= fields. Charfield (max_length=16, Widgets=widgets. TextInput (attrs={'class':'Form-control'})) CLS=Fields . Choicefield (Choices=models. Classes.objects.values_list ('ID','title'), Widgets=widgets. Select (attrs={'class':'Form-control'}) or:classTeacherform (Form): Name= fields. Charfield (max_length=16, Widgets=widgets. TextInput (attrs={'class':'Form-control'})) CLS=Fields . Charfield (widget=widgets. Select (Choices=models. Classes.objects.values_list ('ID','title'), Attrs={'class':'Form-control'}                                    )    -Add Teacher page, form is how to generate multi-select HTML tags of a. Design data validation and HTML formatclassTeacherform (Form): Name= fields. Charfield (max_length=16, Widgets=widgets. TextInput (attrs={'class':'Form-control'})) CLS=Fields . Multiplechoicefield (Choices=models. Classes.objects.values_list ('ID','title'), Widgets=widgets. Selectmultiple (attrs={'class':'Form-control'})            )    -when adding a teacher, how to insert the submitted data into the database two tables (teacher table and the third sheet) Class_list= Obj.cleaned_data.pop ('CLS') Teacher= Models. Teacher.objects.create (* *obj.cleaned_data) Teacher.cls.add (*class_list)-Add teacher success, jump to the teachers page, how to display the Teacher class data (teacher and class information is placed on the third table App01_teacher_cls) ITEM.CLS is a objects object, can be followed by values, All, values_list {% forIteminchTeacher_list%}                    <tr> <td>{{item.id}}</td> <td>{{item.name}}</ Td> <td>                            {% forXinchItem.cls.values_list%} {{x}.1}}                            {% ENDFOR%}                        </td>                {% ENDFOR%}#BUG:Open the Add a teacher (or student) page, and add a class page, and then add a class page to add a new class.    Swipe to add a teacher (or student) page, found that the class drop-down box does not dynamically increase the number of newly added classes. Cause analysis: Appears on class Teacherform and Studentform definitions, taking Teacherform as an exampleclassTeacherform (Form): Name= fields. Charfield (max_length=16, Widgets=widgets. TextInput (attrs={'class':'Form-control'})) CLS=Fields . Multiplechoicefield (Choices=models. Classes.objects.values_list ('ID','title'), Widgets=widgets. Selectmultiple (attrs={'class':'Form-control'}) When instantiating a Teacherform object, because of name, the CLS is a class variable, so these two class variables will not change as long as the object is instantiated after the first generation. When the parent class init function is called, the CLS, name is placed inside the self.fields of the parent class Self.fields= {'name': Name,'CLS': CLS} So the workaround comes out, and at each instantiation of the object, fetch the value of the database to the CLS, re-refresh the contents of the CLS field inside the Self.fieldsclassTeacherform (Form): Name= fields. Charfield (max_length=16, Widgets=widgets. TextInput (attrs={'class':'Form-control'})) CLS=Fields . Multiplechoicefield (#Choices=models. Classes.objects.values_list (' id ', ' title '),            #when the __init__ is redefined, the previous line is unnecessary and a database operation is reducedWidget=widgets. Selectmultiple (attrs={'class':'Form-control'})        )        def __init__(Self, *args, * *Kwargs): Super (Teacherform, self).__init__(*args, * *Kwargs) self.fields['CLS'].widget.choices = models. Classes.objects.values_list ('ID','title') Students likeclassStudentform (Form): Name= fields. Charfield (Max_length=8, min_length=2, Widgets=widgets. TextInput (attrs={'class':'Form-control'})) age= fields. Integerfield (max_value=25, min_value=18, Widgets=widgets. TextInput (attrs={'class':'Form-control'})) Email= fields. Emailfield (widget=widgets. TextInput (attrs={'class':'Form-control'})) cls_id=Fields . Integerfield (#widget=widgets. Select (choices=[(1, ' Shanghai '), (2, ' Beijing ')])                #choices Type is [(), ()]widget=widgets. Select (#Choices=models. Classes.objects.values_list (' id ', ' title '),attrs={'class':'Form-control'})            )                    def __init__(Self, *args, * *Kwargs): Super (Studentform, self).__init__(*args, * *Kwargs) self.fields['cls_id'].widget.choices = models. Classes.objects.values_list ('ID','title')
View Code

[Oldboy-django] [2 in-depth Django] teacher Management--view, add, edit

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.