urls.pyhtml
from django.conf.urls import url from django.contrib import admin from app01 import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^classes/', views.classes), url(r'^add_class/', views.add_class), url(r'^edit_class/(\d+)/', views.edit_class), url(r'^students/', views.students), url(r'^add_student/', views.add_student), url(r'^edit_student/(\d+)/', views.edit_student), url(r'^teachers/', views.teachers), url(r'^add_teacher/', views.add_teacher), url(r'^edit_teacher/(\d+)/', views.edit_teacher), ]
app01.views.py正則表達式
class TeacherForm(Form): tname = fields.CharField(min_length=2) # xx = fields.CharField( # widget=widgets.Select( # choices=models.Classes.objects.values_list('id', 'title'), # attrs={'multiple': 'multiple'} # ) # widget=widgets.SelectMultiple( # choices=models.Classes.objects.values_list('id', 'title'), # ) # ) # xx = fields.ChoiceField( # choices=models.Classes.objects.values_list('id','title'), # widget=widgets.SelectMultiple # ) xx = fields.MultipleChoiceField( choices=models.Classes.objects.values_list('id','title'), widget=widgets.SelectMultiple ) def __init__(self,*args,**kwargs): # 解決數據沒法動態顯示的Bug,推薦使用 super(TeacherForm,self).__init__(*args,**kwargs) self.fields['xx'].widget.choices=models.Classes.objects.values_list('id','title') def teachers(request): tea_list = models.Teacher.objects.all() return render(request, 'teachers.html', {'tea_list': tea_list}) def add_teacher(request): if request.method == 'GET': obj = TeacherForm() return render(request, 'add_teacher.html', {'obj': obj}) else: obj = TeacherForm(request.POST) if obj.is_valid(): xx = obj.cleaned_data.pop('xx') row = models.Teacher.objects.create(**obj.cleaned_data) row.c2t.add(*xx) return redirect('/teachers/') else: print(obj.errors) return render(request, 'add_teacher.html', {'obj': obj}) def edit_teacher(request, nid): if request.method == 'GET': row = models.Teacher.objects.filter(id=nid).first() cls_id = row.c2t.values_list('id') id_list = list(zip(*cls_id))[0] if list(zip(*cls_id)) else [] obj = TeacherForm(initial={'tname': row.tname,'xx': id_list}) return render(request, 'edit_teacher.html',{'obj':obj,'nid':nid}) else: obj = TeacherForm(request.POST) if obj.is_valid(): xx = obj.cleaned_data.pop('xx') models.Teacher.objects.filter(id=nid).update(**obj.cleaned_data) row = models.Teacher.objects.filter(id=nid).first() row.c2t.set(xx) return redirect('/teachers/') else: return render(request, 'edit_teacher.html',{'obj':obj,'nid':nid})
templates.teachers.htmldjango
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>老師列表</h1> <p><a href="/add_teacher/">添加</a></p> <table> <thead> <tr> <td>ID</td> <td>老師姓名</td> <td>任教班級</td> <td>操做</td> </tr> </thead> <tbody> {% for t in tea_list %} <tr> <td>{{ t.id }}</td> <td>{{ t.tname }}</td> <td> {% for item in t.c2t.all %} {{ item.title }} {% endfor %} </td> <td><a href="/edit_teacher/{{ t.id }}/">編輯</a></td> </tr> {% endfor %} </tbody> </table> <ul> </ul> </body> </html>
templates.add_teacher.htmlapp
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>添加老師</h1> <form action="/add_teacher/" method="post"> {% csrf_token %} <p> 姓名:{{ obj.tname }} </p> <p> 班級:{{ obj.xx }} </p> <input type="submit" value="提交"> </form> </body> </html>
templates.edit_teacher.htmlpost
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>編輯老師</h1> <form action="/edit_teacher/{{ nid }}/" method="post" novalidate> {% csrf_token %} <p>{{ obj.tname }}</p> <p>{{ obj.xx }}</p> <input type="submit" value="提交"> </form> </body> </html>
多選框:url
cls_id = fields.MultipleChoiceField( choices=models.Classes.objects.values_list('id','title'), widget=widgets.SelectMultiple )
單選框:code
cls_id_id = fields.IntegerField( widget=widgets.Select( choices=models.Classes.objects.all().values_list('id', 'title'), attrs={'class': 'form-control'} ) ) cls_id_id = fields.IntegerField( widget=widgets.Select( choices=models.Classes.objects.all().values_list('id', 'title'), attrs={'class': 'form-control'} ) )
class TestForm(Form): t1 = fields.CharField( widget=widgets.Textarea(attrs={}) ) t2 = fields.CharField( widget=widgets.CheckboxInput ) t3 = fields.MultipleChoiceField( choices=[(1, 'basketball'),(2, 'football'),(3, 'voliball')], widget=widgets.CheckboxSelectMultiple ) t4 = fields.ChoiceField( choices=[(1,'basketball'),(2,'football'),(3,'voliball')], widget=widgets.RadioSelect ) t5 = fields.CharField( widget=widgets.PasswordInput(attrs={}) ) t6 = fields.FileField( widget=widgets.FileInput ) def test(request): obj = TestForm({'t3': [2, 3], }) return render(request,'test.html',{'obj':obj})
- is_valid - 字段 = 默認正則表達式 - 額外的正則 from django.forms import Form from django.forms import widgets from django.forms import fields from django.core.validators import RegexValidator class MyForm(Form): user = fields.CharField( validators=[RegexValidator(r'^[0-9]+$', '請輸入數字'), RegexValidator(r'^159[0-9]+$', '數字必須以159開頭')], ) - clean_字段,必須返回值 - clean() 有返回值:cleaned_data = 返回值 無返回值:cleaned_data = 原來的值
import os def f1(request): if request.method == 'GET': return render(request, 'f1.html') else: file_obj = request.FILES.get('fafafa') print(type(file_obj)) print(file_obj.name) print(file_obj.size) f = open(os.path.join('static',file_obj.name),'wb') for chunk in file_obj.chunks(): f.write(chunk) f.close() return render(request, 'f1.html') class F2Form(Form): user = fields.CharField() fafafa = fields.FileField() def f2(request): if request.method == 'GET': obj = F2Form() return render(request, 'f2.html',{'obj': obj}) else: file_obj = request.FILES.get('fafafa') obj = F2Form(data=request.POST,files=request.FILES) print(type(file_obj)) print(file_obj.name) print(file_obj.size) f = open(os.path.join('static', file_obj.name), 'wb') if obj.is_valid(): print(obj.cleaned_data) print(obj.cleaned_data.get('fafafa').name) print(obj.cleaned_data.get('fafafa').size) f = open(os.path.join('static', obj.cleaned_data.get('fafafa').name), 'wb') for chunk in obj.cleaned_data.get('fafafa'): f.write(chunk) f.close() return render(request, 'f2.html', {'obj': obj}) else: print(obj.errors) return render(request, 'f2.html', {'obj': obj})