django form表单组建使用及CBV模式
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了django form表单组建使用及CBV模式相关的知识,希望对你有一定的参考价值。
一、FBV和CBV FBV:是视图函数以函数的方式写逻辑代码 CBV:是视图以类的方式写逻辑代码 如CBV: views.py from django.shortcuts import render,HttpResponse,redirect from django.views import View from django.views.decorators.csrf import csrf_exempt,csrf_protect from django.utils.decorators import method_decorator from app01 import models class LoginView(View): @method_decorator(csrf_exempt) def dispatch(self, request, *args, **kwargs): return super(LoginView, self).dispatch(request,*args,**kwargs) def get(self, request, *args, **kwargs): return render(request, ‘login.html‘) def post(self,request, *args, **kwargs): user = request.POST.get(‘user‘) pwd = request.POST.get(‘pwd‘) obj = models.UserInfo.objects.filter(username=user,password=pwd).first() if obj: request.session[‘user_info‘] = {‘id‘: obj.id, ‘username‘: obj.username} return redirect(‘/users.html‘) return render(request, ‘login.html‘, {‘msg‘: ‘去你的‘}) 如FBV: views.py def register(request): if request.method == "GET": form = RegisterForm() return render(request, ‘register.html‘, {‘form‘: form}) else: response = {‘status‘: True, ‘data‘: None, ‘msg‘: None} form = RegisterForm(request.POST) if form.is_valid(): print(form.cleaned_data) else: response[‘status‘] = False response[‘msg‘] = form.errors return HttpResponse(json.dumps(response)) 二、form表单组建使用 导入forms模块 from django.forms import Form from django.forms import fields from django.forms import widgets #创建表单类 class UserForm(Form): username = fields.CharField( required=True, error_messages={‘required‘: ‘用户名不能为空‘}, widget = widgets.TextInput(attrs={‘class‘: ‘form-control‘}) ) password = fields.CharField( required=True, error_messages={‘required‘: ‘邮箱不能为空‘, ‘invalid‘: ‘邮箱格式错误‘}, widget = widgets.TextInput(attrs={‘class‘: ‘form-control‘}) ) ut_id = fields.ChoiceField( choices = [], widget = widgets.Select(attrs={‘class‘: ‘form-control‘}) ) role_id = fields.MultipleChoiceField( choices=[], widget=widgets.SelectMultiple(attrs={‘class‘: ‘form-control‘}) ) def __init__(self, *args, **kwargs): super(UserForm,self).__init__(*args,**kwargs) self.fields[‘ut_id‘].choices = models.UserType.objects.values_list(‘id‘,‘title‘) self.fields[‘role_id‘].choices = models.Role.objects.values_list(‘id‘,‘caption‘) 注: username, password,渲染到页面是input标签属性的name值 fields.CharField表示输入文本类型, fields.ChoiceField表示单选下拉框 fields.MultipleChoiceField表示多选下拉框 required=True表示必填选项 error_messages={‘required‘: ‘用户名不能为空‘}表示错误提示 widget = widgets.TextInput(attrs={‘class‘: ‘form-control‘})表示增加class属性 form表单对页面渲染 views.py class AddUserView(AuthView, View): def get(self, request, *args, **kwargs): form = UserForm() return render(request, ‘add_user.html‘, {‘form‘: form}) def post(self,request,*args,**kwargs): form = UserForm(data=request.POST) if form.is_valid(): role_id_list = form.cleaned_data.pop(‘role_id‘) obj = models.UserInfo.objects.create(**form.cleaned_data) obj.rl.add(*role_id_list) return redirect(‘/users.html‘) else: print(form.errors) return render(request, ‘add_user.html‘, {‘form‘: form}) add_user.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>添加用户</h1> <form method="POST" novalidate> {% csrf_token %} <p> 用户名: {{ form.username }} {{ form.errors.username.0 }} </p> <p> 密码: {{ form.password }} {{ form.errors.password.0 }} </p> <p> 用户类型: {{ form.ut_id }} {{ form.errors.ut_id.0 }} </p> <p> 角色: {{ form.role_id }} {{ form.errors.role_id.0 }} </p> <input type="submit" value="提交"> </form> </body> </html> 注: form.username表示name="username"的input框, form.errors.username.0验证填写是否正确, 错误返回error_messages={‘required‘: ‘用户名不能为空‘}编写的信息 三、登录验证: 继承: 单继承: # class BaseView(View): # def dispatch(self, request, *args, **kwargs): # if request.session.get(‘username‘): # response = super(BaseView,self).dispatch(request, *args, **kwargs) # return response # else: # return redirect(‘/login.html‘) # # class IndexView(BaseView): # # def get(self,request,*args,**kwargs): return HttpResponse(request.session[‘username‘]) 多继承: # 多继承方式: # class BaseView(object): # def dispatch(self, request, *args, **kwargs): # if request.session.get(‘username‘): # response = super(BaseView,self).dispatch(request, *args, **kwargs) # return response # else: # return redirect(‘/login.html‘) # # class IndexView(BaseView,View): # # def get(self,request,*args,**kwargs): # return HttpResponse(request.session[‘username‘]) 装饰器: def auth(func): def inner(request,*args,**kwargs): if request.session.get(‘username‘): obj = func(request,*args,**kwargs) return obj else: return redirect(‘/login.html‘) return inner # @method_decorator(auth,name=‘get‘) class IndexView(View): @method_decorator(auth) def dispatch(self, request, *args, **kwargs): if request.session.get(‘username‘): response = super(IndexView,self).dispatch(request, *args, **kwargs) return response else: return redirect(‘/login.html‘) @method_decorator(auth) def get(self,request,*args,**kwargs): return HttpResponse(request.session[‘username‘]) @method_decorator(csrf_exempt) # 无效 def post(self,request,*args,**kwargs): return HttpResponse(request.session[‘username‘]) 四、序列化 方式一: user_list = models.UserInfo.objects.all() data = serializers.serialize("json", user_list) [ {"model": "app01.userinfo", "pk": 1, "fields": {"username": "\u5174\u666e", "password": "123123"}}, {"model": "app01.userinfo", "pk": 2, "fields": {"username": "\u94f6\u79cb\u826f", "password": "666"}} ] 方式二: user_list = models.UserInfo.objects.values(‘id‘,‘username‘) user_list = list(user_list) data = json.dumps(user_list) [ {"username": "\u5174\u666e", "id": 1}, {"username": "\u94f6\u79cb\u826f", "id": 2} 问题:对json.dumps做定制: import json from datetime import date from datetime import datetime class JsonCustomEncoder(json.JSONEncoder): def default(self, field): if isinstance(field, datetime): return field.strftime(‘%Y-%m-%d %H:%M:%S‘) elif isinstance(field, date): return field.strftime(‘%Y-%m-%d‘) else: return json.JSONEncoder.default(self, field) user_list = [ {‘id‘:1,‘name‘:‘alex‘,‘ctime‘: datetime.now()}, {‘id‘:2,‘name‘:‘eric‘,‘ctime‘: datetime.now()} ] data = json.dumps(user_list,cls=JsonCustomEncoder) print(data) 五、form表单验证总结 1、创建Form类(本质就是正则表达式的集合) from django.forms import Form from django.forms import fields from django.forms import widgets class UserForm(Form): username = fields.CharField( required=True, error_messages={‘required‘:‘用户名不能为空‘}, widget=widgets.TextInput(attrs={‘class‘:‘form-control‘}) ) password = fields.CharField( required=True, error_messages={‘required‘: ‘邮箱不能为空‘,‘invalid‘:‘邮箱格式错误‘}, widget = widgets.TextInput(attrs={‘class‘: ‘form-control‘}) ut_id = fields.ChoiceField( choices=[], widget=widgets.Select(attrs={‘class‘:‘form-control‘}) ) role_id = fields.MultipleChoiceField( choices=[], widget=widgets.SelectMultiple(attrs={‘class‘:‘form-control‘}) ) def __init__(self,*args,**kwargs): super(UserForm,self).__init__(*args,**kwargs) # self.fields已经有所有拷贝的字段 self.fields[‘ut_id‘].choices = models.UserType.objects.values_list(‘id‘,‘title‘) self.fields[‘role_id‘].choices = models.Role.objects.values_list(‘id‘,‘caption‘) 2.只是生成HTML标签: 添加页面 form = MyForm() {{form.xx}} 3. 带默认值的HTML标签: 编辑页面 form = MyForm(initial={‘xx‘: xxx}) {{form.xx}} 4. 提交数据 form = MyForm(data=request.POST) if form.is_valid(): print(form.cleaned_data) else: print(form.errors) 问题:下拉框数据无法实时更新 class UserForm(Form): username = fields.CharField( required=True, error_messages={‘required‘:‘用户名不能为空‘} ) password = fields.CharField( required=True, error_messages={‘required‘: ‘邮箱不能为空‘,‘invalid‘:‘邮箱格式错误‘} ) ut_id = fields.ChoiceField(choices=[]) def __init__(self,*args,**kwargs): super(UserForm,self).__init__(*args,**kwargs) self.fields[‘ut_id‘].choices = models.UserType.objects.values_list(‘id‘,‘title‘)
本文出自 “linux技术” 博客,请务必保留此出处http://haoyonghui.blog.51cto.com/4278020/1964492
以上是关于django form表单组建使用及CBV模式的主要内容,如果未能解决你的问题,请参考以下文章