Django之Form組件補充

1、定義的規則html

class TeacherForm(Form):  #必須繼承Form
    # 建立字段,本質上是正則表達式
    username = fields.CharField( required=True,     #必填字段
        error_messages={"required":"用戶名不能爲空!!"},  #顯示中文錯誤提示
        widget=widgets.TextInput(attrs={"placeholder":"用戶名","class":"form-control"}),  #自動生成input框
        label="姓名", label_suffix=":" ) password = fields.CharField(required=True, error_messages={'required': '密碼不能爲空'}, widget=widgets.PasswordInput(attrs={'placeholder': '密碼', 'class': 'form-control'}), label="密碼", label_suffix=":" ) # 不能爲空
 email = fields.EmailField( required=True, error_messages={"required":"郵箱不能爲空!!","invalid":"無效的郵箱"}, widget=widgets.EmailInput(attrs={"placeholder": "郵箱", "class": "form-control"}),  # 自動生成input框
        label = "郵箱", label_suffix = ":" ) #不能爲空且郵箱格式要一致

 

頁面上渲染正則表達式

用第二種方式須要加上下面的這個:django

 2、下拉框的規則post

 

3、判斷用戶民是不存在,存在就不添加了ui

須要導入:spa

from django.core.exceptions import ValidationError

3d

4、initial  修改時用到code

 

5、自定義配置orm

一、在settings中:htm

二、導入settings

from django.conf import settings

 三、使用

 6、多對多修改兩種方式(用Form)

def editteacher(request,nid): obj = models.UserInfo.objects.filter(id=nid, ut_id=1).first() # print(obj.username)
    if not obj: return redirect("/teacherindex/") if request.method=="GET": print([obj.id for obj in obj.teacher_classes.all()])  #[2] 拿到select框的id是爲了要作默認顯示的
        form = TeacherForm(initial={"username":obj.username,"password":obj.password,"email":obj.email,"teacher_classes":[obj.id for obj in obj.teacher_classes.all()]}) #就讓顯示一個input框,而且帶有原來哪一行的內容
        return render(request, "editteacher.html", {"form":form}) else: form = TeacherForm(data=request.POST) if form.is_valid():#開始校驗,注意這要加括號
            cls_list = form.cleaned_data.pop("teacher_classes") print(cls_list) models.UserInfo.objects.filter(id=nid).update(**form.cleaned_data) obj.teacher_classes.set(cls_list) #更新第三張表
            return redirect("/teacherindex/") else: return render(request, "editteacher.html", {"form":form})

 

 

7、多對多添加(用Form)

def addteacher(request): if request.method=="GET": form = TeacherForm()  #只是讓顯示一個input框
        return render(request, "addteacher.html", {"form":form}) else: form = TeacherForm(data=request.POST) # print(form) #<QuerySet [<UserInfo: UserInfo object>, <UserInfo: UserInfo object>, <UserInfo: UserInfo object>]>
        if form.is_valid():# 開始驗證
            # print('執行成功',form.cleaned_data) # 全部匹配成功,字典
            # # {'username': 'ghf', 'password': '435', 'email': 'fddfg@qq.com', 'teacher_classes': ['4']}
            cls_list = form.cleaned_data.pop("teacher_classes") print("============id",cls_list) form.cleaned_data['ut_id'] = 1
            #建立新老師的對象
            teacher_obj = models.UserInfo.objects.create(**form.cleaned_data) #建立新老師和班級的關係
            teacher_obj.teacher_classes.add(*cls_list)  #之前添加的是對象,如今也能夠吧id添加進去
            return redirect("/teacherindex/") else: # print("=====?",form.errors,type(form.errors))#返回失敗的結果
            # print(form.errors["username"][0]) #拿到返回失敗的結果,渲染到頁面
            return render(request, "addteacher.html", {"form":form})
相關文章
相關標籤/搜索