【发布时间】:2020-04-23 18:59:09
【问题描述】:
有没有办法在“返回重定向”之后发送电子邮件?
views.py
选项 1:在“重定向”之前放置“发送邮件”
if request.method == 'POST':
formset = TestFormSet(request.POST,request.FILES,instance=project)
if formset.is_valid():
subject = 'Notifications'
html_message = "Testing notifications"
recipient = ["testingemail@gmail.com"]
send_mail(subject, html_message, EMAIL_HOST_USER, [recipient],fail_silently = False)
formset.save()
return redirect("home")
With Option 1, the email is sent successfully but on the front-end the page has to wait until the email is sent before the redirection takes place.
选项 2:在重定向后放置“send_mail”
if request.method == 'POST':
formset = TestFormSet(request.POST,request.FILES,instance=project)
if formset.is_valid():
formset.save()
return redirect("home")
subject = 'Notifications'
html_message = "Testing notifications"
recipient = ["testingemail@gmail.com"]
send_mail(subject, html_message, EMAIL_HOST_USER, [recipient],fail_silently = False)
使用选项 2,保存表单集但不发送电子邮件。 有没有办法在重定向后发送电子邮件,以便用户在页面重定向之前无需等待电子邮件处理?
谢谢。
【问题讨论】:
标签: django django-forms django-views django-templates