A Data Migration for Every Django Project

jieforest發表於2012-06-20
How to use a South data migration to avoid accidentally sending emails from example.com.

Problem

Consider the following snippet from Django's docs [1] for sending a confirmation email:

CODE:

from django.contrib.sites.models import Site
from django.core.mail import send_mail

def register_for_newsletter(request):
    current_site = Site.objects.get_current()
    send_mail(
        'Thanks for subscribing to %s alerts' % current_site.name,
        'Thanks for your subscription. We appreciate it.\n\n-The %s team.' % current_site.name,
        'editor@%s' % current_site.domain,
        [user.email]
    )Here the domain for the email sender is taken from the 'current site' instance, which is controlled by Django's 'Sites' framework and accessible by a custom method on the manager of the Site model.

By default, a Site instance is created with domain and display name 'example.com' and you have to correct these values. This is often done by hand using the admin suite.

However, as with any manual change, it's easy to forget and you'll often find Django projects sending email from editor@example.com. Highly embarrassing.




來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-733299/,如需轉載,請註明出處,否則將追究法律責任。

相關文章