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:
- Log in to the Django admin interface.
- Navigate to the "Migrations" section, which is available under the "Django" admin area.
- 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 templatesubject: The rendered subject linebody: The rendered plain text bodyhtml: 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_mail_model_template-0.1.2.tar.gz.
File metadata
- Download URL: django_mail_model_template-0.1.2.tar.gz
- Upload date:
- Size: 9.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7bb3ec1710971ff498b10644e62c20e24f309bdbab0818bb9d568ddf99b2f6d8
|
|
| MD5 |
11758f282e938ca2a8448d473b926241
|
|
| BLAKE2b-256 |
5a2250833401cdd00e2a720173c0b59ed29617f872f8c3d5e3f7054b870cc364
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_mail_model_template-0.1.2.tar.gz -
Subject digest:
7bb3ec1710971ff498b10644e62c20e24f309bdbab0818bb9d568ddf99b2f6d8 - Sigstore transparency entry: 427720828
- Sigstore integration time:
-
Permalink:
dreamiyokoo/django-mail-model-template@ec8c373315284b7d8e44dbf624a0d897186a487c -
Branch / Tag:
refs/heads/main - Owner: https://github.com/dreamiyokoo
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@ec8c373315284b7d8e44dbf624a0d897186a487c -
Trigger Event:
push
-
Statement type:
File details
Details for the file django_mail_model_template-0.1.2-py3-none-any.whl.
File metadata
- Download URL: django_mail_model_template-0.1.2-py3-none-any.whl
- Upload date:
- Size: 8.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e68999db97ba03d9dba11fbd5123d4fa0e538214ea1e1dd2c2d0553095a2c3e5
|
|
| MD5 |
2fa49975ccddad000b9403e416e26a2b
|
|
| BLAKE2b-256 |
b63da23c01fd3129c9a2211dbb5cd6bb23124f04ff39bf66fb02d855efb04dc8
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_mail_model_template-0.1.2-py3-none-any.whl -
Subject digest:
e68999db97ba03d9dba11fbd5123d4fa0e538214ea1e1dd2c2d0553095a2c3e5 - Sigstore transparency entry: 427720829
- Sigstore integration time:
-
Permalink:
dreamiyokoo/django-mail-model-template@ec8c373315284b7d8e44dbf624a0d897186a487c -
Branch / Tag:
refs/heads/main - Owner: https://github.com/dreamiyokoo
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@ec8c373315284b7d8e44dbf624a0d897186a487c -
Trigger Event:
push
-
Statement type: