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.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.2.1
File Size Uploaded
django_bots-1.2.1.tar.gz 45.9 kB Details

Built distribution (wheel)

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

Total release size: 80.8 kB

Release files / django_bots-1.2.1.tar.gz

Download URL django_bots-1.2.1.tar.gz
Size 45.9 kB
Tags Source
SHA-256 checksum
How to use checksums
da14ef75fbe059a20d76852817b19fa347234e580cd543f72781d01dee60874c
BLAKE2b-256 checksum
How to use checksums
4c5be1e2c0ee399796dea574e0a6776484c993ed58ba6852743d1483f35e7e9a
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.2.1-py3-none-any.whl

Download URL django_bots-1.2.1-py3-none-any.whl
Size 34.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
448b5bfdab759a6ca72e1adb2f34785fc8df12ed5d269e40916c6a1ccb856335
BLAKE2b-256 checksum
How to use checksums
a22d85ce94479887ce74d8a29a96c6c80355653292fcc743f092bcbcdf09eb5f
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

This release

1.2.1 This release

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