上一篇:Django之Form验证clean方法(验证篇六)点击跳转
目录篇:Django之Form及ModelForm目录篇 点击跳转
下一篇:Django之Form及ModelForm操作汇总(验证篇八)点击跳转
目录
Django之创建项目【二】:https://blog.csdn.net/Burgess_zheng/article/details/86505526
进入项目目录下
创建app命令:
F:\Burgess\Python\pycharm实验脚本\test\project_burgess>python manage.py startapp burgess_form
路径:project_burgess/project_burgess/urls.py
from django.contrib import admin from django.urls import path,re_path #Django版本2.0以上 from project_burgess import views from django.conf.urls import url #Django2.0 from django.conf.urls import include urlpatterns = [ path('admin/', admin.site.urls), #form re_path('burgess_form/', include("burgess_form.urls")), # url(r'^burgess_form/', include("burgess_form.urls")), ]
路径:project_burgess/burgess_form/urls.py
__author__ = "Burgess Zheng" #!/usr/bin/env python #-*- coding:utf-8 -*- from django.urls import path,re_path #Django版本2.0以上 from burgess_form import views from django.conf.urls import url #Django2.0 urlpatterns = [ re_path('mf/', views.mf), #url(r'^mf/', views.mf), re_path('mf-edit-(\d)/', views.mf_edit), #url(r'^mf_edit/', views.mf_edit), ]
跳转:实战之Django之Model建表流程 https://blog.csdn.net/Burgess_zheng/article/details/86564984
路径:project/modes_handle/models.py
from django.db import models class UserType(models.Model): name = models.CharField(max_length=10) def __str__(self): return self.name class M2M(models.Model): name = models.CharField(max_length=32) def __str__(self): return self.name class User(models.Model): name = models.CharField(max_length=32) email = models.EmailField(max_length=32) user_type = models.ForeignKey('UserType',on_delete=models.CASCADE) # 一对多 m = models.ManyToManyField('M2M') ctime = models.DateTimeField(auto_now_add=True,null=True)
进入项目目录下执行以下面命令(创建表 or 重新生成表结构)
python manage.py makemigrations
python manage.py migratedjango自带的sqlite可视化添加数据
User
UserType
M2M
User_m
1.自定义modeform字段验证类
2.实例化modelform对象(初始化信息)并把该modelsform对象对象返回前端(前端可根据该对象调用字段相关信息)
3.前端提交的数据进行验证提取正确or错误信息及响应错误信息(把form对象返回,前端可以调用验证错误信息)
路径:project_burgess/burgess_form/views.py
from django.shortcuts import render from modes_handle import models from django import forms from django.forms import fields as Ffields #为什么要别名,因为Meta使用的和form字段的fields重复了 from django.forms import widgets as ws #为什么要别名,因为Meta使用的和form字段的widgets重复了 class UserModelForm(forms.ModelForm):#继承ModelForm email = forms.CharField()#该字段和数据库是没关系的 class Meta: model = models.User #调用models文的User表 fields = "__all__" #指定User表里的字段,__all__:所有字段 # fields = ['name']#指定User表里的name字段,也可用元组只要可迭代就行 # exclude =['name'] #排除 labels = { #对字段名修改 'email':'邮箱' } help_texts={ #帮助信息就是一个标签 'email':'*'#一般写*带表必须 } widgets = { # 定制插件 也就是修改标签的类型和添加样式 'name': ws.Textarea(attrs={'class': 'c1'}) } error_messages = { '__all__':{'f':'ddd'},#自定义调用add_errors函数把异常作为实参的错误,也可以在这里修改 #整体的错误自定义最优先 'name':{'required':'必填','invalid':'格式错误'} } #field_classes = {'email':Ffields.URLField} #为该字段换成其他格式验证 #email字段变成URL验证 #localized_fields = ('ctime') #更改时间区 默认UTC def mf(request): if request.method == "GET": obj = UserModelForm() return render(request,'mf.html',{'obj':obj}) elif request.method == "POST": obj = UserModelForm(request.POST) obj.is_valid() ret = obj.is_valid() print(ret) if ret: #print(obj.cleaned_data) #models.User.objects.create(**obj.cleaned_data) #以上是以前models和form分开就需要这样操作 obj.save()#使用modelsform 只需要这句就帮你保存了 else: print(obj.errors) return render(request, 'mf.html', {'obj': obj}) def mf_edit(request,nid): if request.method == "GET": model_obj = models.User.objects.get(id=nid) obj = UserModelForm(instance=model_obj) return render(request,'mf1.html',{'obj':obj,'nid':nid}) elif request.method == "POST": model_obj = models.User.objects.get(id=nid) obj = UserModelForm(request.POST,instance=model_obj) #数据对象导入,等于初始化编辑 obj.is_valid() ret = obj.is_valid() print(ret) if ret: #print(obj.cleaned_data) #models.User.objects.create(**obj.cleaned_data) #以上是以前models和form分开就需要这样操作 obj.save()#obj内部帮我们保存了User该表的数据,也保存了多对多表的数据 #mobj = obj.save(commit=False)#拆分保存 #mobj.save()#单一保存User表 #mobj.save_m2m()#单一保存多对多表 #obj.save() 等于上面3行代码执行的结果 else: print(obj.errors) return render(request, 'mf1.html', {'obj': obj})
涉及csrf:Django之CSRF XSS原理解析【交互篇四】https://blog.csdn.net/Burgess_zheng/article/details/86548425
路径:project_burgess/templates/m1.html
Title 路径:project_burgess/templates/mf1.html
Title
手动:python manage.py runserver 127.0.0.1:8000
pycharm:
get请求添加页面
错误提交
正确提交
查看数据库
get请求编辑页面
错误提交
正确提交
查看数据库
上一篇:Django之Form验证clean方法(验证篇六)点击跳转
目录篇:Django之Form及ModelForm目录篇 点击跳转
下一篇:Django之Form及ModelForm操作汇总(验证篇八)点击跳转