Java教程

Djanjo forms组件

本文主要是介绍Djanjo forms组件,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

forms组件

第一步:定义一个类,继承forms.Form
第二步:在类中写字段,要校验的字段,字段属性就是校验规则
第三步:实例化得到一个Form对象,把要校验的数据传入
第四步:调用register_form.is_valid()校验,校验通过就是True
第五步:校验通过有register_form.cleaned_data
第六步:校验不通过 register_form.errors

校验字段

#定义类
from django import forms
from django.forms import widgets
from django.core.exceptions import ValidationError
class RegisterForm(forms.Form):
    name = forms.CharField(max_length=8, min_length=3, label='用户名')
    password = forms.CharField(max_length=8, min_length=3, label='密码')
    re_password = forms.CharField(max_length=8, min_length=3, label='确认密码')
    email = forms.EmailField(label='邮箱')
   

#在视图中使用
register_form = RegisterForm(request.POST)
if register_form.is_valid():
      # 校验通过,存
      # 取出校验通过的数据
      print('校验通过')
      print(register_form.cleaned_data)
else:
     # 校验不通过
     print('校验不通过')
     print(register_form.errors)

渲染标签

方式一

<h2>通过form自动渲染一</h2>
<form action="" method="post">
    <p>用户名 {{ form.name }}</p>
    <p>密码 {{ form.password }}</p>
    <p>确认密码 {{ form.re_password }}</p>
    <p>邮箱 {{ form.email }}</p>
    <input type="submit" value="提交">
</form>

方式二

<h2>通过form自动渲染二(基本用这种)</h2>
<div class="container-fluid">
    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <form action="" method="post" novalidate>
                {% for item in form %}
                    <div class="form-group">
                        <p>{{ item.label }}{{ item }} 
                            <span style="color: red">{{ item.errors.0 }}</span>
                        </p>
                    </div>
                {% endfor %}
                
                <input type="submit" value="提交">
                <span style="color: red">{{ error }}
                </span>

            </form>
        </div>
    </div>
</div>

方式三

<h2>通过form自动渲染三</h2>
<form action="" method="post">
    {{ form.as_p }}
    {#    {{ form.as_table }}#}
    {#    {{ form.as_ul }}#}
</form>

渲染错误信息

前端

<h2>通过form自动渲染二(基本用这种)</h2>
<div class="container-fluid">
    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <form action="" method="post" novalidate>
                {% for item in form %}
                    <div class="form-group">
                        <p>{{ item.label }}{{ item }} 
                            <span style="color: red">{{ item.errors.0 }}</span>
                        </p>
                    </div>
                {% endfor %}
                <input type="submit" value="提交">
                <span style="color: red">{{ error }}
                </span>

            </form>
        </div>
    </div>
</div>

后端

from django import forms
from django.forms import widgets
from django.core.exceptions import ValidationError

class RegisterForm(forms.Form):
    name = forms.CharField(
        					max_length=8,min_length=3, label='用户名',
                            error_messages={
                            				'max_length': '最长为8', 
                                			'min_length': '最短为3'
                            				},
                            widget=widgets.TextInput(attrs={'class':'form-control'})
    						)
    password = forms.CharField(
        						max_length=8, min_length=3, label='密码',
                                error_messages={'required': '该字段必填'},
                                widget=widgets.PasswordInput(
                                    			attrs={'class':'form-control'})
    											)
    re_password = forms.CharField(
        							max_length=8, min_length=3, label='确认密码',
                                    widget=widgets.PasswordInput(
                                        		attrs={'class':'form-control'})
    											)
    email = forms.EmailField(
        					 label='邮箱',
                             error_messages={
                                 			 'required': '必填', 
                                 			 'invalid': '必须是邮箱格式'
                             				 },
                             widget=widgets.TextInput(attrs={'class':'form-control'}))

forms组件参数配置

max_length=8	    # 最大长度
min_length=3, 		# 最小长度
label='用户名'		  # 别名
error_messages={'max_length': '最长为8', 'min_length': '最短为3'}		# 错误信息
widget=widgets.TextInput(attrs={'class':'form-control'}))			# 样式

全局钩子,局部钩子(总代码)

后端

# 模型层
from django import forms
from django.forms import widgets
from django.core.exceptions import ValidationError

class RegisterForm(forms.Form):
    name = forms.CharField(max_length=8, min_length=3, label='用户名',
                           error_messages={'max_length': '最长为8', 'min_length': '最短为3'},
                           widget=widgets.TextInput(attrs={'class':'form-control'}))
    password = forms.CharField(max_length=8, min_length=3, label='密码',
                               error_messages={'required': '该字段必填'},
                               widget=widgets.PasswordInput(attrs={'class':'form-control'}))
    re_password = forms.CharField(max_length=8, min_length=3, label='确认密码',
                                  widget=widgets.PasswordInput(attrs={'class':'form-control'}))
    email = forms.EmailField(label='邮箱',
                             error_messages={'required': '必填', 'invalid': '必须是邮箱格式'},
                             widget=widgets.TextInput(attrs={'class':'form-control'}))
    
    def clean_name(self):#name字段的局部钩子
        # 校验名字不能以sb开头
        name=self.cleaned_data.get('name')
        if name.startswith('sb'):
            # 校验不通过,必须抛异常,
            raise ValidationError('不能以sb开头')
        else:#校验通过,返回name对应的值
            return name

    def clean(self): # 全局钩子
        password=self.cleaned_data.get('password')
        re_password=self.cleaned_data.get('re_password')
        if re_password==password:
            #校验通过
            return self.cleaned_data
        else:
            raise ValidationError('两次密码不一致')



# 视图层
from app01 import models

def register(request):
    if request.method == 'GET':
        # 生成一个空form对象
        register_form = RegisterForm()
        return render(request, 'register.html', {'form': register_form})

    else:
        # 实例化得到对象,传入要校验的数据
        # register_form=RegisterForm(data=request.POST)
        register_form = RegisterForm(request.POST)
        if register_form.is_valid():
            # 校验通过,存
            # 取出校验通过的数据
            print('校验通过')
            print(register_form.cleaned_data)
            register_form.cleaned_data.pop('re_password')
            models.User.objects.create(**register_form.cleaned_data)
            return HttpResponse('ok')

        else:
            # 校验不通过
            print('校验不通过')
            print(register_form.errors)
            error=register_form.errors.get('__all__')[0]
            print(type(register_form.errors.as_json))
            from django.forms.utils import ErrorDict
            # name_error=register_form.errors.get('name')[0]
            return render(request, 'register.html', {'form': register_form,'error':error})

前端

<h2>通过form自动渲染二(基本用这种)</h2>
<div class="container-fluid">
    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <form action="" method="post" novalidate>
                {% for item in form %}
                    <div class="form-group">
                        <p>{{ item.label }}{{ item }} <span style="color: red">{{ item.errors.0 }}</span></p>
                    </div>

                {% endfor %}
                <input type="submit" value="提交"><span style="color: red">{{ error }}</span>

            </form>
        </div>
    </div>
</div>
这篇关于Djanjo forms组件的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!