Does not involve database storage Data Example one: FORM implementation page drop-down selection box:
url.py
from Import URL from Import Admin from Import = [ # url (r ' ^admin/', admin.site.urls), url (r'^ index/', Views.index),]
views.py
fromDjango.shortcutsImportRender fromDjango.shortcutsImportHttpResponse fromApp01ImportModels fromDjangoImportFormsclassIndexform (forms. Form): C= { (1,'CEO'), (2,'COO')} user_type_id= Forms. Integerfield (widget=forms. Select (choices=c))defIndex (Request): Form=Indexform ()returnRender (Request,'index.html',{'form': Form})
Index.html
<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <title></title></Head><Body><H1>Index</H1>{{form.user_type_id}}</Body></HTML>
Database Storage Data Example two: by models.py a one-to-one appearance correlation method to achieve
models.py
from Import Models # Create your models here. class usertype (models. Model): = models. Charfield (max_length=16)class UserInfo (models. Model): = models. Charfield (max_length=32) = models. Charfield (max_length=32) = models. ForeignKey ('usertype')
Create a database table to execute the following two statements:
views.py to generate data through loops
fromDjango.shortcutsImportRender#From django.shortcuts import HttpResponse fromApp01ImportModels fromDjangoImportFormsclassIndexform (forms. Form):#C = { #(1, ' CEO '), #(2, ' COO ') # }c = Models. UserType.objects.all (). Values_list ('ID','caption') user_type_id= Forms. Integerfield (widget=forms. Select (choices=c))defIndex (Request):For I in Range (Ten): models. UserType.objects.create (caption= ' CE ' +str (i)) C = models. UserType.objects.all (). Count () print (c)form =Indexform ()returnRender (Request,'index.html',{'form': Form})
After the data is generated, modify the viiews.py
fromDjango.shortcutsImportRender#From django.shortcuts import HttpResponse fromApp01ImportModels fromDjangoImportFormsclassIndexform (forms. Form):#C = { #(1, ' CEO '), #(2, ' COO ') # }c = Models. UserType.objects.all (). Values_list ('ID','caption') user_type_id= Forms. Integerfield (widget=forms. Select (choices=c))defIndex (Request):#For I in Range (Ten): #models. UserType.objects.create (caption= ' CE ' +str (i)) #C = models. UserType.objects.all (). Count () #print (c)form =Indexform ()returnRender (Request,'index.html',{'form': Form})
Index.html
<! DOCTYPE html>{{form.user_type_id}}</body>
If there is a problem, if you continue to add new data in the database, the page does not display new data, and if it does, you need to restart the Django
The following methods are used to solve the above problems
Python's "13th chapter" Django FORM