2018-10-03-Python全栈开发-day60-django序列化-part3
联合唯一
clean_字段方法只能对某个字段进行检查,当clean方法执行完之后,最后还会执行clean方法,在clean方法中,可以通过获取数据字典中的值然后进行验证
from django.shortcuts import render,HttpResponse from django import forms from 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_data username=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 Configuration The `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/2.1/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: path('', views.home, name='home') Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') Including another URLconf 1. Import the include() function: from django.urls import include, path 2. 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>

浙公网安备 33010602011771号