Python 【第十三章】 Django 之 FORM
不涉及数据库存储数据例一 :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') } 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
以下方法是解决上述问题
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 __init__(self,*args, **kwargs): # 父类构造方法:1、获取所有静态字段 2、fields = [] super(Indexform, self).__init__(*args, **kwargs) # print(self.fields['user_type_id'].widget.choices) self.fields['user_type_id'].widget.choices = models.UserType.objects.all().values_list('id','caption') 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}) def add_user_type(request): q = request.GET.get('q', None) if q: models.UserType.objects.create(caption=q) return HttpResponse(q)
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), url(r'^add_user_type/',views.add_user_type), ]
add_user_type.html
测试添加数据
http://127.0.0.1:8000/add_user_type/?q=yangjian
实现实时添加 新数据