django-slack Integration

5.19.0 · active · verified Fri Apr 17

django-slack provides easy-to-use integration between Django projects and the Slack group chat and IM tool. It simplifies sending messages and notifications to Slack channels. Currently at version 5.19.0, it is actively maintained with regular updates, typically released multiple times a year to follow Django versions or introduce new features and fixes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to send a simple Slack message. It includes a minimal Django settings configuration to make the snippet runnable outside of a full Django project (e.g., as a standalone script). In a real Django project, ensure 'slack' is in `INSTALLED_APPS` and `SLACK_WEBHOOK_URL` (or `SLACK_TOKEN`) is defined in your `settings.py`.

import os
from django.conf import settings
import django

# Configure Django settings minimally for a standalone script
# In a full Django project, these settings would be in your settings.py
if not settings.configured:
    settings.configure(
        INSTALLED_APPS=['slack'],
        # Replace with your actual Slack Incoming Webhook URL
        # or SLACK_TOKEN if using the full API
        SLACK_WEBHOOK_URL=os.environ.get(
            'SLACK_WEBHOOK_URL',
            'https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX' # Placeholder URL
        ),
        # A secret key is required by Django for settings.configure
        SECRET_KEY='a-secret-key-for-demo-purposes-only',
        DEBUG=True,
        # Minimal database and timezone settings for django.setup() to work
        DATABASES={'default': {'ENGINE': 'django.db.backends.sqlite3', 'NAME': ':memory:',}},
        USE_TZ=True,
        TIME_ZONE='UTC',
    )
    django.setup()

from slack import slack_message

# Send a simple message to a specific Slack channel
# Ensure the channel exists and your webhook/token has permissions
slack_message(
    text='Hello from Django-Slack! This is a quickstart test message.',
    channel='#general', # Replace with your target channel name
    username='DjangoBot',
    icon_emoji=':rocket:'
)

print("Attempted to send message to Slack. Check your configured channel.")
# Note: Actual delivery depends on correct Slack configuration and network access.

view raw JSON →