Python 【第十三章】 Django 之 FORM
不涉及數(shù)據(jù)庫存儲(chǔ)數(shù)據(jù)例一 :FORM ?實(shí)現(xiàn)頁面下拉選擇框:
url.py
?
from django.conf.urls import url from django.contrib import admin from app01 import viewsurlpatterns = [# 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 formsclass 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>?
?
涉及數(shù)據(jù)庫存儲(chǔ)數(shù)據(jù)例二:通過models.py 一對(duì)一外表關(guān)聯(lián)方式來實(shí)現(xiàn)
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')?
創(chuàng)建數(shù)據(jù)庫表執(zhí)行以下兩個(gè)語句:
python manage.py makemigrations python manage.py migrate?
views.py ?通過循環(huán)來生成數(shù)據(jù)
from django.shortcuts import render # from django.shortcuts import HttpResponse from app01 import models from django import formsclass 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})生成 數(shù)據(jù)后,修改viiews.py
from django.shortcuts import render # from django.shortcuts import HttpResponse from app01 import models from django import formsclass Indexform(forms.Form):# c = {# (1,'CEO'),# (2,'COO')# }c = models.UserType.objects.all().values_list('id','caption') #該名在數(shù)據(jù)庫有數(shù)據(jù)后出現(xiàn),就不會(huì)有出錯(cuò)如果數(shù)據(jù)庫在沒有數(shù)據(jù)時(shí),該語句查不到數(shù)據(jù),會(huì)報(bào)錯(cuò)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>?
?
?
?遇到問題,如果在數(shù)據(jù)庫中繼續(xù)添加新數(shù)據(jù),會(huì)出現(xiàn)頁面沒有顯示 新數(shù)據(jù),如果顯示 ,需要重啟django
?
以下方法是解決上述問題
views.py
?
from django.shortcuts import render from django.shortcuts import HttpResponse from app01 import models from django import formsclass Indexform(forms.Form):# c = {# (1,'CEO'),# (2,'COO')# }c = models.UserType.objects.all().values_list('id','caption') # 類中靜態(tài)字段原理,由類直接調(diào)用user_type_id = forms.IntegerField(widget=forms.Select(choices=c))def __init__(self,*args, **kwargs):# 父類構(gòu)造方法:1、獲取所有靜態(tài)字段 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 viewsurlpatterns = [# url(r'^admin/', admin.site.urls),url(r'^index/',views.index),url(r'^add_user_type/',views.add_user_type), ]?
?
add_user_type.html?
?測(cè)試添加數(shù)據(jù)
http://127.0.0.1:8000/add_user_type/?q=yangjian
實(shí)現(xiàn)實(shí)時(shí)添加 新數(shù)據(jù)
?
轉(zhuǎn)載于:https://www.cnblogs.com/yaabb163/p/6354050.html
總結(jié)
以上是生活随笔為你收集整理的Python 【第十三章】 Django 之 FORM的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 算法训练 最大的算式
- 下一篇: Uva 11464 偶数矩阵