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