Django EmailField and full email address with first and last name

I would like to use EmailField in the form. However, instead of saving

support@acme.com

I want to save

"ACME Support" <support@acme.com>

The reason is that when sending emails I would like for a "friendly name" to appear. It can be done?

+3
source share
3 answers

We use the Django email field and then use the property to display the friendly name in the email.

from django.utils.html import escape
from django.utils.safestring import mark_safe

class MyModel(models.Model):
    email_address = models.EmailField()
    full_name = models.CharField(max_length=30)
    ...

    @property
    def friendly_email(self):
        return mark_safe(u"%s <%s>") % (escape(self.fullname), escape(self.email_address))
+3
source

Django EmailFieldcan store the display name directly. You do not need a separate model field.

0
source

Source: https://habr.com/ru/post/1737856/


All Articles