DEV Community

Mr. Glass
Mr. Glass

Posted on

Simple inline django endpoint

In 99% of cases, it is terrible practice to do this. I managed to find the 1% case today.

You need to add a simple endpoint to your django application. Its so simple it doesn't deserve it's controller or even it's own view class. In my case, I had a healthcheck endpoint, that should simply always return 'Success'. Normally an endpoint requires additional functions/classes etc, but since I am just returning a string I wanted to just write my string inline. Turns out you can!

In urls.py, add the following to your routes list:
path('healthcheck', (lambda x:HttpResponse("Success"))),

Make sure you import HttpResponse at the top of the file, if you haven't already:
from django.http import HttpResponse

That's it!

Top comments (0)