Django's form, the data in the HTML display

Source: Internet
Author: User

Django version 1.4.5

For the Charfield inside the models.

Here's how to write

In models.py, add parameters to Charfield

1  fromDjango.dbImportModels2  fromDjango.formsImportModelform3 4Title_choices =  ( 5 (' Mr ', ' Mr. '),6 (' Mrs ', ' Mrs. '),7 (' Ms ', ' Ms. '),8 )9 classAuthor (models. Model):TenName = models. Charfield (max_length=100)  Onetitle = models. Charfield (max_length=3, choices=title_choices) A  -     def  __unicode__(self): -           returnSelf.name the  - classAuthorform (modelform): -      classMeta: -Model = Author
models.py

Which, (' Mr ', ' Mr. '), the previous parameter is stored in the database, the latter is displayed to the user's

In the forms.py

1  fromDjangoImportForms2 3 classAuthorform (forms. Form):4Name = forms. Charfield (max_length=100) 5title = Forms. Charfield (max_length=3,6Widget=forms. Select (choices=title_choices))7 8 #official documents are written in this way, but the result of practice is problematic, "title_choices" does not know9 #so you need to add the following in front of this classTenTitle_choices =  (  One (' Mr ', ' Mr. '), A (' Mrs ', ' Mrs. '), - (' Ms ', ' Ms. '), - ) the #to be added before the call Oh, in addition, you need to keep consistent with the models.py
forms.py

This will have a drop-down box effect when it is called in HTML.

Django's form, the data in the HTML display

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.