About Enumeration-type Choices
Use models.TextChoices
to create enumeration-type choices for your model fields:
from django.db import models
from django.utils.translation import gettext_lazy as _
class Post(models.Model):
class PublishingStatus(models.TextChoices):
DRAFT = "d", _("Draft")
PUBLISHED = "p", _("Published")
publishing_status = models.CharField(
_("Publishing status"),
max_length=1,
choices=PublishingStatus.choices,
default=PublishingStatus.DRAFT,
)
Then you can check the publishing status in the template as follows:
{% load i18n %}
{% if post.publishing_status == post.PublishingStatus.DRAFT %}
<button type="button" id="publish">{% translate "Publish" %}</button>
{% endif %}
Also by me
Django Paddle Subscriptions app
For Django-based SaaS projects.
Django App for You
Django GDPR Cookie Consent app
For Django websites that use cookies.
Django App for You