Skip to main content

Manage email templates in DB with django

Project description

INSTALL

pip install django-mail-model-template

Django settings

INSTALLED_APPS = [
    ...
    'django_mail_model_template',
]

Migrations

Migrations are a way of propagating changes you make to your models (adding a field, deleting a model, etc.) into your database schema. They are designed to be mostly automatic, but you'll need to know when to make migrations, when to run them, and the common issues you might run into.

You can create migrations for your application using the following command:

python manage.py makemigrations

This command will detect changes made to your models and generate the appropriate migrations.

Once the migrations have been created, you can apply them to your database using:

python manage.py migrate

This command applies the migrations and updates the database schema.

Viewing Migrations in the Admin

Django's admin interface allows you to view the state of applied migrations. To do this, follow these steps:

  1. Log in to the Django admin interface.
  2. Navigate to the "Migrations" section, which is available under the "Django" admin area.
  3. Here, you will be able to see a list of migrations and their states (applied or unapplied).

By using these tools, you can manage the evolution of your database schema in a controlled and predictable way.

Usage

Register the template either via django-admin or through code.

from django_mail_model_template.models import MailTemplate

MailTemplate.objects.create(
    name="main",
    subject="main subject {{ name }}",
    body="main body {% if name %}{{ name }}{% endif %}",
    html="<p>main html {{ name }}</p>",
)
from django_mail_model_template.utils import get_mail_template
params = {"name": "yamada"}
result = get_mail_template("main", params)

send html mail

from django_mail_model_template.utils import send_html_mail
params = {"name": "yamada"}
send_html_mail("main", params, "from@example.com",["to@example.com"])

send text mail

from django_mail_model_template.utils import send_text_mail
params = {"name": "yamada"}
send_text_mail("main", params, "from@example.com",["to@example.com"])

Multi-language Support

Setting up templates for different languages

You can create templates for different languages by specifying the language field:

from django_mail_model_template.models import MailTemplate

# English template
MailTemplate.objects.create(
    name="welcome",
    subject="Welcome {{ name }}",
    body="Hello {{ name }}, welcome to our service!",
    html="<p>Hello {{ name }}, welcome to our service!</p>",
    language="en"  # ISO 639-1 language code
)

# Japanese template
MailTemplate.objects.create(
    name="welcome",
    subject="ようこそ {{ name }}さん",
    body="こんにちは、{{ name }}さん。当サービスへようこそ!",
    html="<p>こんにちは、{{ name }}さん。当サービスへようこそ!</p>",
    language="ja"
)

Using templates with language specification

You can specify the language when retrieving templates:

from django_mail_model_template.utils import get_mail_template

# Get template in English
params = {"name": "John"}
result = get_mail_template("welcome", params, language="en")

# Get template in Japanese
params = {"name": "山田"}
result = get_mail_template("welcome", params, language="ja")

If a template is not available in the specified language, it will fall back to the default language (as specified in settings.LANGUAGE_CODE), or to English if that's not available.

Important Note About Return Values

As of version X.X.X, get_mail_template() now returns a MailTemplateParams object instead of a dictionary. Access the values using attribute notation instead of dictionary notation:

# Old (dictionary) syntax - NO LONGER WORKS
result = get_mail_template("welcome", params)
subject = result["subject"]
body = result["body"]

# New (object) syntax - CURRENT USAGE
result = get_mail_template("welcome", params)
subject = result.subject
body = result.body
html = result.html
name = result.name

The MailTemplateParams object has the following attributes:

  • name: The name of the template
  • subject: The rendered subject line
  • body: The rendered plain text body
  • html: The rendered HTML body

Model Configuration Note

When using this library with multiple languages, ensure your MailTemplate model does not have unique=True on the name field, as this would prevent creating the same template name in different languages. The model should use unique_together for the name and language fields instead.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_mail_model_template-0.1.2.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_mail_model_template-0.1.2-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file django_mail_model_template-0.1.2.tar.gz.

File metadata

File hashes

Hashes for django_mail_model_template-0.1.2.tar.gz
Algorithm Hash digest
SHA256 7bb3ec1710971ff498b10644e62c20e24f309bdbab0818bb9d568ddf99b2f6d8
MD5 11758f282e938ca2a8448d473b926241
BLAKE2b-256 5a2250833401cdd00e2a720173c0b59ed29617f872f8c3d5e3f7054b870cc364

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_mail_model_template-0.1.2.tar.gz:

Publisher: ci.yml on dreamiyokoo/django-mail-model-template

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_mail_model_template-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for django_mail_model_template-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e68999db97ba03d9dba11fbd5123d4fa0e538214ea1e1dd2c2d0553095a2c3e5
MD5 2fa49975ccddad000b9403e416e26a2b
BLAKE2b-256 b63da23c01fd3129c9a2211dbb5cd6bb23124f04ff39bf66fb02d855efb04dc8

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_mail_model_template-0.1.2-py3-none-any.whl:

Publisher: ci.yml on dreamiyokoo/django-mail-model-template

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page