码迷,mamicode.com
首页 > 编程语言 > 详细

Python 【第十三章】 Django 之 FORM

时间:2017-01-28 00:31:12      阅读:285      评论:0      收藏:0      [点我收藏+]

标签:blog   dmi   response   pat   use   model   生成   没有   pre   

不涉及数据库存储数据例一 :FORM  实现页面下拉选择框:

url.py

 

from django.conf.urls import url
from django.contrib import admin
from app01 import views

urlpatterns = [
    # url(r‘^admin/‘, admin.site.urls),
    url(r^index/,views.index),
]

 

 

 

views.py

 

from django.shortcuts import render
from django.shortcuts import HttpResponse
from app01 import models
from django import forms

class Indexform(forms.Form):
    c = {
        (1,CEO),
        (2,COO)
    }
    user_type_id = forms.IntegerField(widget=forms.Select(choices=c))

def index(request):
    form = Indexform()
    return render(request,index.html,{form:form})


 

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<h1>index</h1>
{{ form.user_type_id }}

</body>
</html>

 

技术分享

 

涉及数据库存储数据例二:通过models.py 一对一外表关联方式来实现

models.py

from django.db import models

# Create your models here.

class UserType(models.Model):
    caption = models.CharField(max_length=16)

class UserInfo(models.Model):
    user = models.CharField(max_length=32)
    pwd = models.CharField(max_length=32)
    user_type = models.ForeignKey(UserType)

 

创建数据库表执行以下两个语句:

python manage.py makemigrations
python manage.py migrate 

 

views.py  通过循环来生成数据

from django.shortcuts import render
# from django.shortcuts import HttpResponse
from app01 import models
from django import forms

class Indexform(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))

def index(request):
    for i in range(10):
        models.UserType.objects.create(caption=‘CE‘+str(i))
    c = models.UserType.objects.all().count()
    print(c)
    form = Indexform()
    return render(request,index.html,{form:form})

生成 数据后,修改viiews.py

from django.shortcuts import render
# from django.shortcuts import HttpResponse
from app01 import models
from django import forms

class Indexform(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))

def index(request):
    # for i in range(10):
    #     models.UserType.objects.create(caption=‘CE‘+str(i))
    # c = models.UserType.objects.all().count()
    # print(c)
    form = Indexform()
    return render(request,index.html,{form:form})

 

 

 

 

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<h1>index</h1>
{{ form.user_type_id }}

</body>
</html>

 

 技术分享

 

 遇到问题,如果在数据库中继续添加新数据,会出现页面没有显示 新数据,如果显示 ,需要重启django

 

以下方法是解决上述问题

 

Python 【第十三章】 Django 之 FORM

标签:blog   dmi   response   pat   use   model   生成   没有   pre   

原文地址:http://www.cnblogs.com/yaabb163/p/6354050.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!