Python's "13th chapter" Django FORM

Source: Internet
Author: User

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

Related Article

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.