Amiya Behera
Amiya Behera

Reputation: 2270

How to send email in post_save without asking user to wait in form submission?

Here is my post_save method:

from asgiref.sync import sync_to_async

@receiver(post_save, sender=SubmissionDetails)
def create_submission_email(sender, instance, created, **kwargs):
    if created:
        data = sync_to_async(call_submssion_email(instance))

def call_manuscript_submssion_email(instance):
    print("Hi")
    message = '''Submitted with the title, <br>
    {0}'''.format(instance.title)
    subject = 'Article Submitted'
    to_address = '[email protected]'
    from_address = "[email protected]"
    msg = EmailMessage(
                    subject, message,from_address,[to_address]
                    )
    msg.content_subtype = "html"
    msg.send()

The problem is while submitting the form, the user has to wait till email is sent to see the results. I am using django 3.0 with some async support.

Upvotes: 0

Views: 61

Answers (1)

Muhammad Faizan Fareed
Muhammad Faizan Fareed

Reputation: 3748

Asynchronous support

New in Django 3.0

Django has developing support for asynchronous (“async”) Python, but does not yet support asynchronous views or middleware; they will be coming in a future release.

There is limited support for other parts of the async ecosystem; namely, Django can natively talk ASGI, and some async safety support.

Django Asynchronous support official


You can use Django Channels or Celery for your asynchronous code.

You can also check How are Django channels different than celery?

Upvotes: 1

Related Questions