Python 重构Django FormView上下文

Python 重构Django FormView上下文,python,django,sendmail,formview,Python,Django,Sendmail,Formview,我目前以管理员的身份给自己发送电子邮件,以便与客户联系。我不知道如何简单地将所有表单变量传递给Django电子邮件模板。相反,我不得不单独地定义这个问题。有没有一种方法可以传递所有的上下文变量,这样我就不必定义它们中的每一个 form_class = CustomProjectBuildRequestForm success_url = reverse_lazy('home') success_message = "Form successfully submitted!" def form_

我目前以管理员的身份给自己发送电子邮件,以便与客户联系。我不知道如何简单地将所有表单变量传递给Django电子邮件模板。相反,我不得不单独地定义这个问题。有没有一种方法可以传递所有的上下文变量,这样我就不必定义它们中的每一个

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('request-custom-project/email_template.html')
    content = template.render(context)

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

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

from.cleaned_data
应为(或类似于
OrderDict
的变体)。上下文需要一个类似于dict的
对象。因此,您可以简单地使用
cleaned_data
作为
context
content=template.render(form.cleaned_data)

如果您需要模板中的一些附加值,我建议如下

   context = {
       'some': 'extra',
       'values': 1,
   }
   context.update(form.cleaned_data)
   content = template.render(context)

from.cleaned_data
应为(或类似于
OrderDict
的变体)。上下文需要一个类似于dict的
对象。因此,您可以简单地使用
cleaned_data
作为
context
content=template.render(form.cleaned_data)

如果您需要模板中的一些附加值,我建议如下

   context = {
       'some': 'extra',
       'values': 1,
   }
   context.update(form.cleaned_data)
   content = template.render(context)