Skip to main content

🛠️ Set up a project

This guide builds a blog project with Content Studio from an empty folder: settings from environment variables, the media library, a first studio, a dashboard, users and the status page. Each step links to the page with all the options. To add Content Studio to an existing project, start at Configure Django; the steps are the same.

1. Create the project​

Create a Django project called mysite with an app called blog, and install Content Studio with the extras for images and a task worker:

pip install "django-content-studio[images,huey]"
django-admin startproject mysite .
python manage.py startapp blog

[images] makes the image sizes of the media library and [huey] runs background tasks on a worker. For production you add [server] later; see the deploying guide.

2. Configure Django​

Environment settings set the secret key, database, cache, task queue and mail from environment variables, so the same settings run in development and production.

  1. In mysite/settings.py, replace SECRET_KEY, DEBUG, ALLOWED_HOSTS and DATABASES with a call to configure(), and add the apps. The rest of the file, such as MIDDLEWARE and TEMPLATES, stays as startproject wrote it. A fragment of the file:

    # mysite/settings.py
    from pathlib import Path

    from content_studio.contrib.settings import configure

    BASE_DIR = Path(__file__).resolve().parent.parent

    CONTENT_STUDIO = {
    "ADMIN_SITE": "mysite.studio.admin_site",
    }

    configure(globals(), base_dir=BASE_DIR)

    INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "rest_framework",
    "content_studio",
    "content_studio.media_library",
    "content_studio.history",
    "content_studio.contrib.settings",
    "content_studio.contrib.status",
    "huey.contrib.djhuey",
    "blog",
    ]

    Keep django.contrib.admin installed: Content Studio records its activity log with it. configure() also points the media library at the built-in models of content_studio.media_library, and huey.contrib.djhuey runs the task worker.

  2. Set the one required variable, in an .env file your shell or IDE loads:

    # .env
    DJANGO_SECRET_KEY=a-long-random-value
    DJANGO_DEBUG=1

    Without DATABASE_URL, the project uses SQLite in db.sqlite3.

  3. Mount the admin. admin/ is the usual prefix; move the classic Django admin elsewhere if you keep it:

    # mysite/urls.py
    from django.conf import settings
    from django.conf.urls.static import static
    from django.urls import include, path

    urlpatterns = [
    path("admin/", include("content_studio.urls")),
    ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

    static() serves uploads while DEBUG is on; in production, your web server or storage serves them.

3. Create the site​

The site holds what is not tied to one model: the dashboard, the menu and the extensions. Create it in mysite/studio.py, the module ADMIN_SITE points at:

# mysite/studio.py
from content_studio.admin import ContentStudioSite
from content_studio.dashboard import Dashboard
from content_studio.dashboard.activity_log import ActivityLogWidget


class BlogStudioSite(ContentStudioSite):
dashboard = Dashboard(widgets=[ActivityLogWidget()])


admin_site = BlogStudioSite()

manage.py init_content_studio writes a starting studio.py for you; see Getting started.

4. Add a model and its studio​

Write a plain Django model. A MediaField picks its image from the media library:

# blog/models.py
from django.db import models

from content_studio.media_library.fields import MediaField


class Article(models.Model):
class Status(models.TextChoices):
DRAFT = "draft", "Draft"
PUBLISHED = "published", "Published"

title = models.CharField(max_length=200)
slug = models.SlugField(unique=True)
cover = MediaField()
body = models.TextField(blank=True)
status = models.CharField(
max_length=20, choices=Status.choices, default=Status.DRAFT
)
published_at = models.DateTimeField(null=True, blank=True)

def __str__(self):
return self.title

Register it with a studio in blog/admin.py:

# blog/admin.py
from content_studio import register
from content_studio.admin import ModelStudio
from content_studio.form import FormSet

from .models import Article


@register(Article)
class ArticleStudio(ModelStudio):
icon = "ph-bold ph-article"
table_display = ["title", "status", "published_at"]
list_filter = ["status"]
search_fields = ["title"]
prepopulated_fields = {"slug": ("title",)}
edit_main = [
FormSet(title="Content", fields=["title", "slug", "cover", "body"]),
]
edit_sidebar = ["status", "published_at"]

The forms guide and the displaying content guide take the edit page and the list page further.

5. Migrate, check and log in​

python manage.py makemigrations blog
python manage.py migrate
python manage.py check
python manage.py createsuperuser
python manage.py runserver

check runs the system checks, which report most configuration mistakes, such as a required field missing from the edit page. Open /admin/ and log in with the superuser. The menu shows Articles, the media library, and users and groups; System status is in the user menu.

6. Invite editors​

Editors are staff users with model permissions, as in the classic Django admin. Add them under Users in the admin: Content Studio emails an invite to set their password, and you pick their permissions in a matrix or through a group. See User management and Access control.

Until you set EMAIL_URL, emails such as the invite are printed to the console of the development server.

7. Run the worker​

Without a task queue, background tasks such as notification digests and backups run during the request. Give them a queue in the database and start the Huey worker next to the server:

# .env
TASKS_URL=db://
python manage.py migrate
python manage.py run_huey

The system status page shows whether the worker runs, and the tasks it ran.

Next steps​