Python 【第十三章】 Django 之 FORM
生活随笔
收集整理的這篇文章主要介紹了
Python 【第十三章】 Django 之 FORM
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
不涉及數據庫存儲數據例一 :FORM ?實現頁面下拉選擇框:
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>?
?
涉及數據庫存儲數據例二:通過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 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})生成 數據后,修改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') #該名在數據庫有數據后出現,就不會有出錯如果數據庫在沒有數據時,該語句查不到數據,會報錯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 formsclass 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 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?
?測試添加數據
http://127.0.0.1:8000/add_user_type/?q=yangjian
實現實時添加 新數據
?
轉載于:https://www.cnblogs.com/yaabb163/p/6354050.html
總結
以上是生活随笔為你收集整理的Python 【第十三章】 Django 之 FORM的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 算法训练 最大的算式
- 下一篇: Uva 11464 偶数矩阵