django-slack Integration
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
-
ModuleNotFoundError: No module named 'django_slack'
cause Attempting to import from the old package name 'django_slack' after upgrading to version 4.0.0 or later.fixChange all imports from `from django_slack import ...` to `from slack import ...` (e.g., `from slack import slack_message`). -
ImproperlyConfigured: Either SLACK_TOKEN or SLACK_WEBHOOK_URL must be defined in your settings.
cause Neither a Slack API token nor an incoming webhook URL has been configured in your Django settings.fixAdd `SLACK_TOKEN = 'xoxb-YOUR-SLACK-BOT-TOKEN'` or `SLACK_WEBHOOK_URL = 'https://hooks.slack.com/services/...'` to your `settings.py`. -
AttributeError: module 'slack.signals' has no attribute 'slack_message_pre_send'
cause You are trying to connect to a signal that was removed or renamed in django-slack version 5.0.0.fixUpdate your signal connections to use the new `slack.signals.message_sending` or `slack.signals.message_sent` signals. Consult the documentation for their updated signatures. -
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='hooks.slack.com', port=443): Max retries exceeded with url: ...
cause Indicates a network issue, an incorrect `SLACK_WEBHOOK_URL` or `SLACK_TOKEN`, or a firewall blocking outbound requests to Slack.fixVerify your `SLACK_WEBHOOK_URL` or `SLACK_TOKEN` is correct. Check network connectivity from your server to Slack. Ensure no firewall rules are preventing outgoing HTTPS connections to `hooks.slack.com` or `slack.com`.
Warnings
- breaking The `slack.signals.slack_message_pre_send` and `slack.signals.slack_message_post_send` signals were removed in version 5.0.0.
- breaking The `SLACK_INCOMING_WEBHOOK_URL` setting was renamed to `SLACK_WEBHOOK_URL` for consistency in version 5.0.0.
- gotcha The primary import path for modules changed from `django_slack` to `slack` in version 4.0.0.
- gotcha Understanding the difference between `SLACK_TOKEN` and `SLACK_WEBHOOK_URL` is crucial for correct setup.
Install
-
pip install django-slack
Imports
- slack_message
from django_slack import slack_message
from slack import slack_message
- slack_notify
from slack.api import slack_notify
- Message
from slack.messages import Message
Quickstart
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.