---
title: "Django"
description: "Generate dynamic OG images and on-demand graphics in Django with the Imejis.io API. A secure view that keeps your API key server-side and returns a cached image."
url: "https://www.imejis.io/apis/django"
published: "2026-07-27"
---

# Django

## Django

Generate a unique OG image for every page, or any on-demand graphic, from a single Imejis template. The pattern below uses a Django **view** as a thin proxy: your API key stays on the server, and the browser (and social crawlers) only ever see your own URL.

Set two environment variables first: `IMEJIS_API_KEY` (from your dashboard) and `IMEJIS_DESIGN_ID` (any design's ID).

```bash title=".env"
IMEJIS_API_KEY=your_api_key
IMEJIS_DESIGN_ID=your_design_id
```

### 1. A view that renders the image

This view takes a `title` query param, POSTs it to Imejis as a `component.property` override, and returns the finished image bytes. Because the key is sent server-side, it never reaches the client.

```python showLineNumbers title="views.py"
import os

import requests
from django.http import HttpResponse


def og_image(request):
    title = request.GET.get("title", "Untitled")

    response = requests.post(
        f"https://render.imejis.io/v1/{os.environ['IMEJIS_DESIGN_ID']}",
        headers={
            "dma-api-key": os.environ["IMEJIS_API_KEY"],
            "Content-Type": "application/json",
        },
        json={"headline.text": title},
    )
    response.raise_for_status()

    res = HttpResponse(response.content, content_type="image/jpeg")
    # Renders are deterministic, so cache aggressively at the edge/CDN.
    res["Cache-Control"] = "public, immutable, no-transform, max-age=31536000"
    return res
```

Wire it up with a route in `urls.py`.

```python showLineNumbers title="urls.py"
from django.urls import path

from . import views

urlpatterns = [
    path("og", views.og_image, name="og_image"),
]
```

### 2. Point your metadata at it

Reference the view from your template's `<head>`, passing each page's real data. Social platforms fetch `/og?title=…` from your own domain, no key exposed.

```html showLineNumbers title="templates/base.html"
<meta
  property="og:image"
  content="https://your-site.com/og?title={{ post.title|urlencode }}"
/>
```

**Override any field.** Each dynamic component exposes keys like `headline.text`, `title.color`, or `image.image`. Send only the ones you want to change. A design's overridable fields are listed in its Share / API panel.
