Django: CSRF exempt view

Django’s CSRF protection is usually a great thing, but when building (API) endpoints meant to be accessed by scripts/third parties it gets in the way of that.

This is how to disable it:

For a class based view

from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt

@method_decorator(csrf_exempt, name='dispatch')
class MyView(View):
    pass

For a function based view

from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def my_view(request):
    pass

Handling signals with Python

When building a Python script that is long running or has to manage some state on termination it is helpful to handle a couple of signals: SIGINT: The signal sent when pressing Ctrl+C SIGTERM and SIGQUIT: Meant to terminate the process, sent by kill and process managers like systemd or supervisord Handling them is possible with Pythons signals library: import signals class SignalHandler: stop = False def __init__(self): # Ctrl+C signal. [Read More]
python 

Local S3 with MinIO in Django

In production I would consider it best practice to use a S3 solution for serving assets. Namely static files and user-generated media. This describes my setup on how to do this locally too. The main benefit for me is that there is less of a difference between environments and I can test S3 specific features in my app. Setup I will assume a already working Django project and MacOS with [[brew]] installed, but brew specific parts are easilly replicated on different systems using their native package managers. [Read More]
python  django  s3  minio 

Python

These days Python is my language of choice for both simple scripts and more complex backend applications, usually in combination with [[django]]