Skip to main content

django-bots

PyPI version Python versions CI Documentation

More and more of the traffic on a website comes from crawlers, scrapers and AI bots instead of people. I wrote django-bots to tell them apart in Django, and to keep out the AI bots I don't want.

django-bots tells you who is on the other end of a request: which browser, operating system and device, whether it's a crawler, and whether it's an AI bot. It can serve a robots.txt that disallows AI bots and turn them away with a 403. Each feature is opt-in, and the package adds no models or migrations.

Installation

python -m pip install django-bots

Add the app to INSTALLED_APPS:

INSTALLED_APPS = [
    ...,
    "django_bots",
]

User agents

Add the middleware, and every request gets a lazily parsed request.user_agent:

MIDDLEWARE = [
    ...,
    "django_bots.middleware.UserAgentMiddleware",
]
def home(request):
    if request.user_agent.is_mobile:
        ...
    request.user_agent.browser  # Browser(family="Mobile Safari", version=(5, 1), version_string="5.1")
    request.user_agent.os  # OperatingSystem(family="iOS", version=(5, 1), version_string="5.1")
    request.user_agent.device  # Device(family="iPhone", brand="Apple", model="iPhone")

Templates get the same checks as filters:

{% load bots %}

{% if request|is_mobile %}
  <a href="/app/">Get the app</a>
{% endif %}

Parsing uses ua-parser 1.x with an in-process cache, and works for sync and async views.

Crawlers

is_crawler matches the patterns from crawler-user-agents, which cover search engines, SEO tools, uptime monitors and HTTP libraries:

if request.user_agent.is_crawler:
    ...
{% if not request|is_crawler %}
  <script src="/analytics.js"></script>
{% endif %}

AI bots

is_ai_bot and ai_bot match the list from ai.robots.txt, which is bundled with the package:

if request.user_agent.is_ai_bot:
    logger.info("AI bot: %s", request.user_agent.ai_bot)  # "GPTBot"

Serve a robots.txt that disallows every AI bot:

from django.urls import path

from django_bots.views import robots_txt

urlpatterns = [
    path("robots.txt", robots_txt),
]

robots.txt only asks. To refuse AI bots outright, add the blocking middleware near the top of MIDDLEWARE:

MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "django_bots.middleware.AIBotBlockMiddleware",
    ...,
]

It blocks bots that collect training data, AI search crawlers and chatbot fetches. Coding and browsing agents such as Cursor and Operator get through by default, because they send the same user agent as people using those apps. BOTS_AI_BLOCK_CATEGORIES changes that.

BOTS_AI_ALLOW keeps the bots you want, for both robots.txt and the middleware:

BOTS_AI_ALLOW = ["OAI-SearchBot"]

A daily workflow checks for new ai.robots.txt releases, and data updates ship as patch releases.

Upgrading from django-user-agents

The request.user_agent attributes, the django_bots.utils helpers and the user_agents template filters work the same way as in django-user-agents, so upgrading comes down to new app, middleware and import paths. One thing behaves differently: is_bot is also true for crawlers and AI bots. The migration guide walks through each step.

Compatibility

Python Django
3.10, 3.11 5.2
3.12, 3.13, 3.14 5.2, 6.0, 6.1

Data sources and licenses

django-bots is released under the MIT license. It uses:

django_bots.DATA_VERSIONS shows which versions are in use.

Documentation

The full documentation is at djangobots.readthedocs.io:

Metadata

Release files for django-bots 1.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-bots 1.2.2
File Size Uploaded
django_bots-1.2.2.tar.gz 46.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-bots 1.2.2
File Interpreter ABI Platform
django_bots-1.2.2-py3-none-any.whl Python 3 none any Details

Total release size: 81.6 kB

Release files / django_bots-1.2.2.tar.gz

Download URL django_bots-1.2.2.tar.gz
Size 46.4 kB
Tags Source
SHA-256 checksum
How to use checksums
432c2739520cc9cd6f8e94eec04f84a8c6f833c0720e9af6e26e576af17cb283
BLAKE2b-256 checksum
How to use checksums
247d05fed56fd5845d8cd1c3caa3eb3378b86c3e0e66f4db2fde01e15024b3c2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.13

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 3, 2026.

Transparency log

Release files / django_bots-1.2.2-py3-none-any.whl

Download URL django_bots-1.2.2-py3-none-any.whl
Size 35.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c1130e37b37a62a27b7434bdc8fc5d42f2569edf647b9fc596f8b5b655140272
BLAKE2b-256 checksum
How to use checksums
63d1d7ebd5f023519cc94e077e0af150502f142b2990f2261b99ab3ce2c765c9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.13

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 3, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

1.2.2 This release

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page