0

我正在尝试编写一个函数来获取表单值及其标签,这样我就可以将它们放在这样的电子邮件正文中:

<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,
    })
4

4 回答 4

1

更好的方法是创建模板并使用 django 的render_to_string来获取 html 字符串作为正文。

您可以传递form.cleaned_data给模板以填充模板变量。

于 2012-10-04T09:53:34.067 回答
1

You can render the email body in a separate template. Use the following if using Django 1.3 or newer:

{% for field in form %}
<p><strong>{{ field.label }}</strong>: {{ field.value }}</p>
{% endfor %}

For more information refer to the BoundForm documentation in Django.

于 2012-10-04T10:00:46.493 回答
0

您可以通过托盘将所需的 html 呈现到 body 变量中。您的邮件正文还需要一个 html 模板:

from django.template.loader import render_to_string
body = render_to_string('yourapp/mail_template.html', {'var1':var1, 'var2':var2})

您可以将所需的任何内容传递给模板上下文字典(例如,我使用了 var1 和 var2)。然后像这样发送邮件:

msg = EmailMessage(subject, body, sender, recipients)
msg.send()

Hope this leads into the right direction.

于 2012-10-04T09:58:36.723 回答
0

Yes. render_to_string did the trick!

Here is my updated source:

forms.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')

views.py

from __future__ import unicode_literals
from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.core.mail import send_mail, EmailMessage, EmailMultiAlternatives
from norskfamilie.forms import HealthForm
from django.utils.encoding import smart_unicode
from django.template.loader import render_to_string

def show_Myform(request):
    if request.method == 'POST':

        form = MyForm(request.POST)

        if form.is_valid():
            subject = "My subject"

            sender = form.cleaned_data['email']

            recipients = ['post@mydomain.com']

            rendered = render_to_string('email_body.html', {'form':form , 'form_headline':subject})

            msg = EmailMultiAlternatives(subject, rendered, sender, recipients)
            msg.attach_alternative(rendered, "text/html")
            msg.send()            
            return HttpResponseRedirect('/thanks/')


    else:
        form = MyForm()

    return render(request, 'form/myform.html', {
        'form': form,
    })

email_body.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head>
    <body>
        <h1>{{ form_headline }}</h1>

        {% for field in form %}
            <p><strong>{{ field.label }}</strong>: {{ field.value }}</p>
        {% endfor %}
    </body>
</html>
于 2012-10-04T10:40:24.843 回答