Stand With Ukraine

Getting Started

Installation and Initial Setup

Step 1: Install the Module

To begin using django-content-settings, first install it using pip:

pip install django-content-settings

Step 2: Update settings.py in Your Django Project

After installation, you need to add content_settings to the INSTALLED_APPS list in your Django project's settings.py file. The updated INSTALLED_APPS might look like this:

INSTALLED_APPS = (
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.sites",
    "django.contrib.staticfiles",
    "django.contrib.messages",
    "content_settings", # <-- update
    "books",
)

Step 3: Configure Templates Context Processor

For using variables in templates, add content_settings.context_processors.content_settings to the context_processors in the TEMPLATES configuration. Your TEMPLATES setting might look like this:

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "APP_DIRS": True,
        "OPTIONS": {
            "context_processors": [
                "django.contrib.auth.context_processors.auth",
                "django.contrib.messages.context_processors.messages",
                "django.template.context_processors.request",
                "content_settings.context_processors.content_settings", # <-- update
            ],
        },
    },
]

Step 4: Access Variables in Templates

Now, you can use the variables in templates like this:

<b>{{ CONTENT_SETTINGS.MY_VAR }}</b>

Step 5 (optional): Configure Preview on Site

Add preview on site middleware "content_settings.middlewares.preivew_on_site" to the settings.py to be able to see the changes live, before applying those for all users.

MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "content_settings.middlewares.preivew_on_site", # <-- update (after AuthenticationMiddleware)
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.middleware.clickjacking.XFrameOptionsMiddleware",
]

Step 6 (optional): API Access Configuration

To access variables through the API, update urls.py with the following line:

path("content-settings/", include("content_settings.urls")),

Your urls.py may look like this now:

from django.urls import path, include
from django.contrib import admin

urlpatterns = [
    path("admin/", admin.site.urls),
    path("content-settings/", include("content_settings.urls")), # <-- update
]

After this configuration, run your project along with the necessary migrations.

Creating Your First Variable

Step 1: Define the Variable

Create a file named content_settings.py in any of your working apps, for example, books/content_settings.py. Add the following content:

from content_settings.types.basic import SimpleString

TITLE = SimpleString(
    "Book Store",
    help="The title of the book store",
)

Step 2: Run Migrations

Execute migrations to add this value to the database, allowing you to edit it subsequently.

python manage.py migrate

Understanding the Code

  • TITLE: The name of the variable you will use in your code and admin panel.
  • SimpleString: The type of variable, in this case, a simple string.
  • "Book Store": The default value for this variable.
  • "The title of the book store": A description displayed in the admin panel.

Usage in Code and Templates

In Python Code

To use the variable in Python code, such as in views:

from content_settings.conf import content_settings
content_settings.TITLE

In Templates

In Django templates, access it like this:

<head>
<title>{{ CONTENT_SETTINGS.TITLE }}</title>
</head>

And that's it! You're now ready to use django-content-settings in your Django project, effectively managing editable variables through the admin panel and API.