[Oldboy-django] [2 in-depth Django] student management (Form)--edit (Set input tag properties, set input label default display value, set input type)

Source: Internet
Author: User

1 Django Background implementation set input tag properties, set input label default display value, set input input box type

#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)
View Code

2 student_list.html on edit tab

<href=item.  ID}}class="Glyphicon glyphicon-pencil" > Edit </a>|     

3 Edit Student URL

URL (R ' ^edit_student/nid= (? p<nid>\d+) $ ', views.edit_student),

4 views

defedit_student (Request, nid):ifRequest.method = ="GET": Student= Models. Student.objects.filter (Id=nid). VALUES ('name',' Age','Email','cls_id'). First () obj= Studentform (initial=student)returnRender (Request,'app01_edit_student.html', {'obj': obj,'nid': Nid}) Else: obj=Studentform (Request. POST)ifobj.is_valid ():#Update DatabaseModels. Student.objects.filter (Id=nid). Update (* *obj.cleaned_data)returnredirect'/app01/students')        Else:            returnRender (Request,'app01_edit_student.html', {'obj': obj})
View Code

5 templates

<!DOCTYPE HTML><HTMLLang= "en"><Head>    <MetaCharSet= "UTF-8">    <title>Title</title>    <Linkrel= "stylesheet"href= "/static/plugins/bootstrap-3.3.7-dist/css/bootstrap.css">    <Linkrel= "stylesheet"href= "/static/plugins/font-awesome-4.7.0/css/font-awesome.css"></Head><Body><Divstyle= "width:500px; margin:0 Auto">    <h4style= "margin-left:20px;" >Edit Student</h4>    <formclass= "Form-horizontal"Action= "/app01/edit_student/nid={{Nid}}"Method= "POST">{% Csrf_token%}<Divclass= "Form-group">            <labelclass= "Col-sm-2 Control-label">Name:</label>            <Divclass= "Col-sm-10">{{obj.name}}{{obj.errors.name.0}}</Div>        </Div>        <Divclass= "Form-group">            <labelclass= "Col-sm-2 Control-label">Age:</label>            <Divclass= "Col-sm-10">{{Obj.age}} {{obj.errors.age.0}}</Div>        </Div>        <Divclass= "Form-group">            <labelclass= "Col-sm-2 Control-label">Mailbox:</label>            <Divclass= "Col-sm-10">{{Obj.email}} {{obj.errors.email.0}}</Div>        </Div>        <Divclass= "Form-group">            <labelclass= "Col-sm-2 Control-label">Class:</label>            <Divclass= "Col-sm-10">{{obj.cls_id}} {{obj.errors.cls_id.0}}</Div>        </Div>        <Divclass= "Form-group">            <Divclass= "Col-sm-offset-2 col-sm-10">                <inputtype= "Submit"class= "Btn Btn-default"value= "Submit"/>            </Div>        </Div>    </form></Div></Body></HTML>
View Code

[Oldboy-django] [2 in-depth Django] student management (Form)--edit (Set input tag properties, set input label default display value, set input type)

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.