[英]Refactor Django FormView Context
I currently email myself submitted form information as an admin so I can contact a customer. 我目前以管理员身份通过电子邮件发送自己提交的表单信息,因此我可以联系客户。 I could not figure out how to simply pass all of the form variables to the Django Email Template.
我不知道如何简单地将所有表单变量传递给Django电子邮件模板。 I instead had to define the individually.
我不得不单独定义。 Is there a way to pass all of the context variables so I don't have define each and every one of them?
有没有一种方法可以传递所有上下文变量,所以我没有定义每个变量?
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
should be a simple dictionary (or a variant like OrderDict
). from.cleaned_data
应该是一个简单的字典 (或类似OrderDict
的变体)。 The template expects a dict
-like object as context. 模板需要一个类似
dict
的对象作为上下文。 Hence you could simply use cleaned_data
as context
: content = template.render(form.cleaned_data)
. 因此,您可以简单地使用
cleaned_data
作为context
: content = template.render(form.cleaned_data)
。
If you need some additional values in the template, I suggest the following 如果您需要模板中的一些其他值,建议您执行以下操作
context = {
'some': 'extra',
'values': 1,
}
context.update(form.cleaned_data)
content = template.render(context)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.