python - 当用户提交表单时,Django 博客向管理员发送电子邮件

标签 python django forms sendmail

背景

我正在建立一个关于我的研究小组的博客。有一个 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',
    '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="90f6e2fffdd0f5e8f1fde0fcf5bef3fffd" rel="noreferrer noopener nofollow">[email protected]</a>',
    ['<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f78398b7928f969a879b92d994989a" rel="noreferrer noopener nofollow">[email protected]</a>'],
    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 = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cbafaea6a48bfaf9fde5a8a4a6" rel="noreferrer noopener nofollow">[email protected]</a>'
    EMAIL_HOST_PASSWORD = 'demo'
    DEFAULT_FROM_EMAIL = 'demo <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6703020a08275655514904080a" rel="noreferrer noopener nofollow">[email protected]</a>>'

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中的表单值可以随邮件一起发送,以便管理员可以查看更多信息。

最佳答案

您可以设置电子邮件格式以发送详细信息。您可以根据您的要求添加更多详细信息。下面是一个例子。

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),
    '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ddbbafb2b09db8a5bcb0adb1b8f3beb2b0" rel="noreferrer noopener nofollow">[email protected]</a>',
    ['<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="11657e517469707c617d743f727e7c" rel="noreferrer noopener nofollow">[email protected]</a>'],
    fail_silently=False,
)

关于python - 当用户提交表单时,Django 博客向管理员发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46039054/

相关文章:

html - 如何在不使用 !important 的情况下自定义 twitter bootstrap 的各种输入大小?

python - 最佳实践 : how do you list required dependencies in your setup. py?

python - 在 spyder 中导入 basemap 时出错

python - 我可以在 1 个域上使用 2 个不同版本的 Python 拥有 2 个 Django 站点吗?

Django DRF : read_only_fields not working properly

javascript - 使用prototype.js停止表单提交

python - 使用 BLAS/ATLAS 预构建 numpy?

python - 为什么不同的Python模块安装在不同的位置?

django - 如何从Django图像字段转换为PIL图像并返回?

html - 在 iOS 版 Safari 中使用“开始”按钮提交表单