[Oldboy-django] [2 in-depth Django] student Management (form)-Add (Beautify form form: Add attributes to front-end tags via form)

Source: Internet
Author: User
Tags html tags

1 Add a label to the Student_list,

<p><href="/app01/add_student"  class="btn btn-primary" > Add </a></ p>      

2 URLs Distribution routes

URL (R ' ^app01/', include (' App01.urls ')), 
URL (R ' ^add_student$ ', views.add_student),

3 Front End

<!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:25px;">Add student</h4>    <formclass= "Form-horizontal"Action= "/app01/add_student"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

4 views

defadd_student (Request):ifRequest.method = ='GET': obj=Studentform ()returnRender (Request,'app01_add_student.html', {'obj': obj}) Else: obj=Studentform (Request. POST)ifobj.is_valid (): Models. Student.objects.create (**obj.cleaned_data)returnredirect'/app01/students')        Else:            returnRender (Request,'app01_add_student.html', {'obj': obj})
View Code

5 Data Rule class settings

classStudentform (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'})    )
View Code

6 Supplement: How to Implement by Form form: Add attributes such as class to the front end tag, and set the Type property of the input box

#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 a dictionary E. Setting the default display value for front-end input via form 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

  

[Oldboy-django] [2 in-depth Django] student Management (form)-Add (Beautify form form: Add attributes to front-end tags via form)

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.