我正在建立一个关于我的研究小组的博客。有一个join_us页面,用户可以在其中填写申请表以申请研究计划。基本功能运作良好但我想在有人提交申请时通知我的导师。
Django关于sending_email的官方文档有点含糊不清,我仍然不知道如何将表单的价值添加到电子邮件内容中。
views.py
def application_action(request):
last_name = request.POST.get('last_name', 'LAST_NAME')
first_name = request.POST.get('first_name', 'FIRST_NAME')
email_address = request.POST.get('email_address', 'EMAIL_ADDRESS')
program_type = request.POST.get('program_type', 'PROGRAM_TYPE')
personal_statement = request.POST.get('personal_statement', 'PERSONAL_STATEMENT')
resume = request.FILES.get('resume')
models.Application.objects.create(last_name=last_name, first_name=first_name, program_type=program_type,
email_address=email_address, personal_statement=personal_statement,
resume=resume)
send_mail(
'An new application',
'Dear professor, you received an new application. Please check it out in the administration console',
'from@example.com',
['to@example.com'],
fail_silently=False,
)
application = models.Application.objects.all()
return render(request, 'research_spacecoupe/join_us.html', {'application': application})
settings.py:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = False
EMAIL_HOST = 'smtp.126.com'
EMAIL_PORT = 25
EMAIL_HOST_USER = 'demo@126.com'
EMAIL_HOST_PASSWORD = 'demo'
DEFAULT_FROM_EMAIL = 'demo <demo@126.com>'
models.py
class Application(models.Model):
program_choice = (('硕士项目', '硕士'), ('博士项目', '博士'), ('博士后项目', '博士后'))
first_name = models.CharField(max_length=30, default="名")
last_name = models.CharField(max_length=30, default="姓")
email_address = models.CharField(max_length=30, null=True)
program_type = models.CharField(max_length=10, choices=program_choice)
personal_statement = models.TextField(null=True)
application_time = models.DateTimeField(auto_now=1)
resume = models.FileField(upload_to='resumes')
def application_info(self):
return '%s %s %s' % (self.first_name, self.last_name, self.program_type)
我希望HTML中的表单值可以与邮件一起发送,以便管理员可以查看更多信息。
答案 0 :(得分:2)
您可以格式化电子邮件以发送详细信息。您可以根据自己的要求添加更多详细信息。以下是一个例子。
models.Application.objects.create(last_name=last_name, first_name=first_name, program_type=program_type,
email_address=email_address, personal_statement=personal_statement,
resume=resume)
send_mail(
'An new application',
'Dear professor, you received an new application. Please check it out in the administration console details are as follows' +
': first_name: {}, last_name: {}'.format(first_name, last_name),
'from@example.com',
['to@example.com'],
fail_silently=False,
)