Python web framework

Django OG Image Integration

Add automatic MyOG.social images to Django templates with request.build_absolute_uri, base templates, and per-view metadata.

Prerequisites

  • A Django site with public pages rendered through templates
  • The request context processor enabled for templates that need request
  • A MyOG.social account with your domain added

How MyOG fits Django

Django projects commonly put social metadata in a base template, then override pieces from child templates or view context.

MyOG needs the absolute public page URL. Django's request.build_absolute_uri gives you that URL when request is available in the template context.

For canonicalized slugs, language prefixes, or query-stripped pages, pass a canonical_url value from the view and encode that instead.

1

Choose a layout

MyOG detects page content and branding automatically. The layout only controls image composition.

2

Add the Django code

base.html snippet

Use this in your shared head template.

{% firstof canonical_url request.build_absolute_uri as page_url %}
<meta property="og:image" content="https://api.myog.social/og?url={{ page_url|urlencode }}&template=screenshot-right" />
<meta property="og:image:width" content="1200" />
<meta property="og:image:height" content="630" />
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:image" content="https://api.myog.social/og?url={{ page_url|urlencode }}&template=screenshot-right" />

If your template engine cannot call request.build_absolute_uri here, compute page_url in the view and pass it in context.

3

Place it correctly

  1. Find base.html or the include that renders shared head metadata.
  2. Add a MyOG block that uses canonical_url when supplied, otherwise request.build_absolute_uri.
  3. Override the block in views that already have custom social image behavior.
  4. Deploy, then inspect the initial HTML returned for list and detail pages.

Avoid duplicate og:image tags

If your base template, CMS plugin, or SEO package already outputs og:image, switch that source to MyOG or ensure child templates do not render a second competing image tag.

Dynamic pages

Detail views can pass canonical_url in context when the public URL should differ from the raw request URL. For ordinary public routes, request.build_absolute_uri is enough.

Test the result

  • Use curl or View Source against the deployed URL and confirm og:image is in the HTML response.
  • Check one list page and one detail page.
  • Verify forwarded host/protocol settings if Django runs behind a proxy.

Start with the MyOG Open Graph Checker, then refresh social platform caches if needed.

Known limitations

The request object is only available in templates when the proper context processor is enabled.
Admin, preview, and login-protected pages should not be used for public MyOG images.
Incorrect proxy headers can make absolute URLs use the wrong scheme or host.

Sources

Ready to get started?

Sign up for free and add generated OG images to Django pages.

Already have an account?

cdf733b534ea2f2ed964d150330b323c44837e4f