How to Be a Teapot in Django

Lift me up, and pour me out.

HTCPCP, or Hyper Text Coffee Pot Control Protocol, was published as an April Fool’s joke 24 years ago today. It’s an HTTP extension for controlling coffee pots, and whilst it is a joke, it appears in various places around the web.

HTCPCP adds one HTTP response code, “418 I’m a teapot”, which teapots can respond with if they are asked to brew coffee. As a registered status code, it has even made its way into the Python standard library’s HTTPStatus:

In [1]: from http import HTTPStatus

In [2]: HTTPStatus.IM_A_TEAPOT
Out[2]: <HTTPStatus.IM_A_TEAPOT: 418>

You can use HTTPStatus with Django to create a teapot page on your site. This will let you follow Google’s google.com/teapot page, so you can be one step closer to web scale!

Throw a view in your project like:

from http import HTTPStatus

from django.shortcuts import render


def teapot(request):
    return render(
        request,
        "teapot.html",
        status=HTTPStatus.IM_A_TEAPOT,
    )

Add a URL definition, such as at teapot/, and make a snazzy template. For a demo, I downloaded a sweet teapot GIF from gifcities.org:

Demo “I'm a teapot” page with image of teapot pouring

Great. That will be very useful, I’m sure.

Fin

May your tea always be perfectly brewed,

—Adam


Improve your Django develompent experience with my new book.


Subscribe via RSS, Twitter, or email:

One summary email a week, no spam, I pinky promise.

Tags: