带有FormView的Django send_mail()

时间:2018-12-27 01:53:33

标签: python django forms django-views sendmail

我正在寻找一种使用send_mail()函数在邮件中呈现所有提交的表单数据的方法。我认为必须有一种方法可以渲染整个提交的表单数据,例如{'form':form},而不是调用所有不同的模型。向我展示如何重构它的任何帮助,将不胜感激。

class PMPIndex(ListView, FormView, SuccessMessageMixin):
template_name = 'pmp/index.html'
model = Post

form_class = CustomProjectBuildRequestForm
success_url = reverse_lazy('home')
success_message = "Form successfully submitted!"

def form_valid(self, form):
    form.save()

    context = {
        'first_name': form.cleaned_data.get('first_name'),
        'last_name': form.cleaned_data.get('last_name'),
        'email': form.cleaned_data.get('email'),
        'phone': form.cleaned_data.get('phone_number'),
        'zip_code': form.cleaned_data.get('zip_code'),
        'project_title': form.cleaned_data.get('project_name'),
        'project_description': form.cleaned_data.get('project_description'),
        'contact_method': form.cleaned_data.get('preferred_contact_method'),
    }

    template = get_template('custom_project_request_email_template.html')
    content = template.render(context)

    send_mail(
        'New Custom Project Request',
        html_message=content,
        message=content,
        from_email=context['email'],
        recipient_list=['kyle@postmyproject.com'],
        fail_silently=False,
    )

    return super(PMPIndex, self).form_valid(form)

0 个答案:

没有答案