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",
    ...,
]

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.0.1

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.0.1
File Size Uploaded
django_bots-1.0.1.tar.gz 40.3 kB Details

Built distribution (wheel)

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

Total release size: 71.5 kB

Release files / django_bots-1.0.1.tar.gz

Download URL django_bots-1.0.1.tar.gz
Size 40.3 kB
Tags Source
SHA-256 checksum
How to use checksums
d5465bd573f23b8906ea846bbf85bd9a997921e1ba13ca5b287037401f0f9c91
BLAKE2b-256 checksum
How to use checksums
2fcccd9746ad1262228fe86be3b6c539bc45930e6d45a9a2b8b46c9f33ad5af2
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 Sep 27, 2026.

Transparency log

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

Download URL django_bots-1.0.1-py3-none-any.whl
Size 31.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
edaff679f3f70fc097c7af28fd7114bc1f08bb272b7f014d17acb2a0a41fe34b
BLAKE2b-256 checksum
How to use checksums
e207292d9a96d33ea7c6948a32101dde1a59e8f30b9e402da14e33c2be911dda
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 Sep 27, 2026.

Transparency log

Release history Release notifications | RSS feed

1.2.2

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

This release

1.0.1 This release

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