Ever wanted to test your Django endpoints before the front end is ready? You sure can.
Turns out Django's testing tools aren't just for TestCase classes:
from django.test import Client
client = Client()
This little object is where all the magic begins - a simple yet powerful gateway for simulating requests.
Need to hit endpoints that require authentication? No problem:
from django.contrib.auth import get_user_model
from django.test import Client
client = Client()
user = get_user_model().objects.get(username="my-username")
client.force_login(user)
With that, you're logged in and ready to make requests.
Here's a quick example of a GET call:
from django.contrib.auth import get_user_model
from django.test import Client
client = Client()
user = get_user_model().objects.get(username="my-username")
client.force_login(user)
client.get("http://localhost:8000/my-endpoint")
Running into issues with host validation? Specify the server name explicitly:
from django.contrib.auth import get_user_model
from django.test import Client
client = Client()
user = get_user_model().objects.get(username="my-username")
client.force_login(user)
client.get("http://localhost:8000/my-endpoint", SERVER_NAME="localhost")
And that's it: a direct way to play with Django endpoints in your local environment.