Disable DRF Browsable API

Django Rest Framework includes a UI for interacting with your API. Here’s how to turn it off. This is a screenshot of the UI from the docs. It’s pretty cool! But, I don’t want it in production. Add this to your settings file to force JSON rendering in non-debug mode– Django-speak for production. # settings.py if not DEBUG: REST_FRAMEWORK["DEFAULT_RENDERER_CLASSES"] = [ "rest_framework.renderers.JSONRenderer", ] This will render a JSON object instead of the UI.

December 3, 2025

Django Routing 101

Something I’ve taken a while to get under my fingers is Django’s URL routing conventions. Here’s a brief summary. At the root of your app or apps, you’ll find a file like this. # urls.py from django.urls import path from . import views urlpatterns = [ path("articles/2003/", views.special_case_2003), path("articles/<int:year>/", views.year_archive), path("articles/<int:year>/<int:month>/", views.month_archive), ] On a bigger app, these path calls might look like this: path("api/posts/", include(posts_urls.urlpatterns)), Which are include-ing from other nested urls.py. I think this convention is a little less direct than Rails’ giant routes.rb, but it also keeps the URLs in nice buckets with the rest of their supporting code.

November 7, 2025

Unpacking MessagePack

SALT messages are encoded with a library called msgpack, the Python implementation of MessagePack. How can we read them? Here’s an example message I want to decode: †£jid´20250602114126621578¦return£tcp§retcode�¢idABC-LAPTOP-02£funªconfig.get¨fun_args‘©transport We can do that with the same library: import msgpack with open('return.p', 'rb') as f: unp = msgpack.unpackb(f.read()) print(unp) Make sure you install msgpack: pip install msgpack This will print something we can read (newlines added by me): { 'jid': '20250602114126621578', 'return': 'tcp', 'retcode': 0, 'id': 'ABC-LAPTOP-02', 'fun': 'config.get', 'fun_args': ['transport'] }

June 5, 2025

Python String Interpolation

Python string interpolation is available via few of interfaces, as Chris at Hashrocket taught me. Today I learned about formatted string literals, more commonly known as f-strings. >>> animals = 'cats' >>> print(f'My house is full of {animals}.') My house is full of cats. They’re my preferred technique because they’re similar to the interpolation syntax I’ve come to know in JavaScript, Ruby, and Elixir. F-String Docs

May 27, 2025

Print Python Function Definition With Getsource

If I’ve defined a function in the Python REPL, I can read its definition with inspect.getsource. >>> def greet(name): print(f"Hello, {name}!") >>> import inspect >>> inspect.getsource(greet) 'def greet(name):\n print(f"Hello, {name}!")\n' And print it, too: >>> print(inspect.getsource(greet)) def greet(name): print(f"Hello, {name}!")

May 16, 2025

Python REPL Help

In the Python REPL, the help function provides a help message about its argument. >>> help(print) Help on built-in function print in module builtins: print(*args, sep=' ', end='\n', file=None, flush=False) Prints the values to a stream, or to sys.stdout by default. It’s a wrapper for pydoc.help. And it works on objects, too: >>> list = ["one", "two"] >>> help(list) Help on list object: class list(object) | list(iterable=(), /) | | Built-in mutable sequence. Learn more by reading help’s help: ...

May 16, 2025