Django

Last updated:

|Edit this page

PostHog makes it easy to get data about traffic and usage of your Django app. Integrating PostHog enables analytics, custom events capture, feature flags, and more.

This guide walks you through integrating PostHog into your Django app using the Python SDK.

Installation

To start, run pip install posthog to install PostHog’s Python SDK.

Then, initialize PostHog where you'd like to use it. For example, here's how to capture an event in a simple view:

views.py
from django.http import HttpResponse
from posthog import Posthog
def home(request):
posthog = Posthog('<ph_project_api_key>', host='<ph_instance_address>')
posthog.capture('disticint_id_of_your_user', 'route_called')
return HttpResponse(f"""
<!DOCTYPE html>
<html>
<body>
<h1>Django docs</h1>
</body>
</html>
""")

You can find your project API key and instance address in your project settings.

Next steps

For any technical questions for how to integrate specific PostHog features into Django (such as analytics, feature flags, A/B testing, etc.), have a look at our Python SDK docs.

Alternatively, the following tutorials can help you get started:

Questions?

Was this page useful?

Next article

Docusaurus

This is a community integration that is not maintained by the PostHog core team. Install or How to use This will automatically start tracking pageviews, clicks and more. For more instructions, see the browser JS library .

Read next article