form表单做验证(一)

forms.py

              (1)

               from django import forms

               class SumForm(forms.Form):
                a = forms.IntegerField(label="num_a")

                b = forms.EmailField(label='Email_b')

               (2)

               

from django import forms
class EmailForm(forms.Form):
      #required
      #label 标签文本
      #IntegerField自带widget,就是Form表单输入框的类型及相关属性,自带number类型
      #虽然
      #max_length min_length
      #error_message 自定义错误信息
      subject = forms.IntegerField(required=True,label='邮件主题',widget=forms.TextInput(attrs={'class':'form-control','placeholder':'请输入邮件主题',
                                                                                      'style':'display:None'}),error_messages={'invalid':'必须填入数字!'})
      message = forms.CharField(required=True,label='邮件内容',widget=forms.Textarea(
          attrs={'class':'form-control','placeholder':'请输入邮件内容'}))
      #收件人地址
      sender= forms.EmailField(required=True,label='收件人地址',widget=forms.TextInput(
          attrs={'class':'form-control','placeholder':'请输入邮件地址'}),error_messages={'invalid':"请填入正确的邮箱地址"})
      #布尔类型
      myself = forms.BooleanField(required=False,label='是否想自己发送')
views.py文件

      

from django.shortcuts import render,HttpResponse
from .forms import EmailForm
from django.core.mail import send_mail,send_mass_mail
from dtest import settings
# Create your views here.
def send_email(request):

    if request.method == 'GET':
        form = EmailForm()
        return render(request,'email_index.html',{"form":form})
    elif request.method == 'POST':
        form = EmailForm(request.POST)
        # print(form)
        #验证表单数据是否合法
        is_ok = form.is_valid()
        if is_ok:
            #执行发送邮件
            subject = form.cleaned_data['subject']
            message = form.cleaned_data['message']
            sender = form.cleaned_data['sender']
            myself = form.cleaned_data['myself']
            to_list = []
            if myself:
                to_list.append(settings.EMAIL_HOST_USER)
            res = send_email(subject,message,settings.EMAIL_HOST_USER,to_list)
        if res == 1:
            return HttpResponse('邮件发送成功!')
        else:
            #信息回填
            return render(request,'email_index.html',{'form':form,'error':'发送失败'})

模板文件:

     

<body>
    <div class="container">
      {#   form标签和button提交按钮标签需要自己定义     #}
      <form class="form-signin" action="{% url 'send_eamil' %} " method="post">
        <h2 class="form-signin-heading">发送邮件</h2>
          {% csrf_token %}
          <div class="form-group">
              <label for="{{ form.subject.id_for_label }}">{{ form.subject.label }}</label>
              {{ form.subject }}
              {{ form.subject.errors }}
          </div>
          <div class="form-group">
              <label for="{{ form.message.id_for_label }}">{{ form.message.label }}</label>
              {{ form.message }}
              {{ form.message.errors }}
          </div>
          <div class="form-group">
              <label for="{{ form.sender.id_for_label }}">{{ form.sender.label }}</label>
              {{ form.sender }}
              {{ form.sender.errors }}
          </div>
          <div class="form-group">
              <label for="{{ form.myself.id_for_label }}">{{ form.myself.label }}</label>
              {{ form.myself }}
              {{ form.myself.errors }}
          </div>
          {# 邮件主题  邮件内容 接收地址 #}
{#          {{ form }}#}
{#          {{ form.subject }}#}
        <button class="btn btn-lg btn-primary btn-block" type="submit">点击发送</button>
      </form>
    </div> <!-- /container -->
    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <script src="../../assets/js/ie10-viewport-bug-workaround.js"></script>
  </body>
<body>
<form action="#">
    {{ form }}
{#    {{ form.as_p }}#}
{#    {{ form.as_ul }}#}
{#    {{ form.as_table }}#}
    <button type="submit">提交</button>
</form>
</body>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值