联合唯一
clean_字段方法只能对某个字段进行检查,当clean方法执行完之后,最后还会执行clean方法,在clean方法中,可以通过获取数据字典中的值然后进行验证
from django.shortcuts import render,HttpResponsefrom django import formsfrom django.forms import fields# Create your views here. #联合唯一class Form1(forms.Form):username=fields.CharField(max_length=32)email =fields.CharField(max_length=32)def clean_username(self):print('this is clean_')return self.cleaned_data['username']def clean(self):value_dict=self.cleaned_datausername=value_dict.get('username')email=value_dict.get('email')if username=='ye' and email=='hai':print('this is clean')return value_dict def index(request):if request.method=='GET':obj=Form1()return render(request,'index.html',{'obj':obj})if request.method=='POST':obj=Form1(request.POST)if obj.is_valid():print('hahah')return HttpResponse('提交成功')else:return render(request,'index.html',{'obj':obj})
from django.db import models# Create your models here.class UserInfo(models.Model):username = models.CharField(max_length=32)email = models.EmailField(max_length=32)
"""untitled URL ConfigurationThe `urlpatterns` list routes URLs to views. For more information please see:https://docs.djangoproject.com/en/2.1/topics/http/urls/ Examples: Function views1. Add an import: from my_app import views2. Add a URL to urlpatterns: path('', views.home, name='home') Class-based views1. Add an import: from other_app.views import Home2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') Including another URLconf1. Import the include() function: from django.urls import include, path2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin from django.urls import path,re_path from app01 import views urlpatterns = [path('admin/', admin.site.urls),re_path(r'index',views.index) ]
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title> </head> <body> <form action="/index" method="post">{{ obj.username }}{{ obj.email }}<input type="submit" value="提交"> </form> </body> </html>