2018-10-03-Python全栈开发-day60-django序列化-part3
生活随笔
收集整理的這篇文章主要介紹了
2018-10-03-Python全栈开发-day60-django序列化-part3
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
聯合唯一
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}) view 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) models """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) ] urls <!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> templates
?
轉載于:https://www.cnblogs.com/hai125698/p/9739752.html
總結
以上是生活随笔為你收集整理的2018-10-03-Python全栈开发-day60-django序列化-part3的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux ssh Unused,安装o
- 下一篇: 使用 Python 多处理库处理 3D