我正在尝试编写一个函数来获取表单值及其标签,这样我就可以将它们放在这样的电子邮件正文中:
<p><strong>Labelvalue</strong>: formvalue</p>
那可能吗?
当我执行“body = smart_unicode(form.cleaned_data)”时,我得到了 dict,但我不确定我能做些什么来获得我想要的 html。
表格.py
class MyForm(forms.Form):
TYPE_CHOICES = (
(ADULT, 'Adult'),
(CHILD, 'Child'),
)
type = forms.ChoiceField(widget=RadioSelect, choices=TYPE_CHOICES, label='Adult or child')
name = forms.CharField(label='Name')
birthdate = forms.DateField(widget=SelectDateWidget(years=range(2012,1900,-1)), label='Birthdate')
address = forms.CharField(label='Address')
email = forms.EmailField(label='Email')
视图.py
def show_myform(request):
if request.method == 'POST':
form = MyForm(request.POST)
if form.is_valid():
subject = "Testsubject"
sender = form.cleaned_data['email']
recipients = ['post@mydomain.com']
body = smart_unicode(form.cleaned_data)
msg = EmailMessage(subject, body, sender, recipients)
msg.send()
return HttpResponseRedirect('/thanks/')
else:
form = MyForm()
return render(request, 'form/myform.html', {
'form': form,
})