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>