Django Rest Framework
Let’s write some
Poetry
Audience
People who already know Django but don’t know Django Rest framework.
Though, you can still proceed if you know zero Django.
Introduction
I’m planning on writing a series on Django Rest Framework. The series aims to provide a very concise introduction to the framework. We would kickstart the series by discussing dependency management using poetry (check out uv for a great alternative). Grab the code, and let’s write some Poetry.
Why Django Rest framework?
Django works great with its template system but we might need to define APIs to work with Javascript frameworks. And, DRF provides this functionality.
Creating a project
Assume that we now have Poetry installed. Note that I’m on Linux but most of what we are about to do works on other platforms.
Create a directory named dog-house.
Run
cd dog-house poetry init
to initialize the project directory.
The prompt would pop up. We then provide important fields as below:
Package name [dog-house]: # Press Enter Compatible Python versions [>=3.12]: # Press Enter Would you like to define your main dependencies interactively? (yes/no) [yes] no Would you like to define your development dependencies interactively? (yes/no) [yes] no
Note that some non-important fields are left out.
We then create a file named poetry.toml with the following content:
[virtualenvs] in-project = true
This will flag the creation of the directory .venv locally.
We can add dependencies by running poetry add <package-name>.
poetry add Django poetry add djangorestframework poetry add django-rest-knox
The package name is available on the pypy.org.
To create a Django project, we run
poetry run django-admin startproject mysite cd mysite poetry run python startapp dog_house
Note that mysite is our main Django app, in the sense that it’s the only app that contains settings.py. We’ll touch this in the last part, you don’t have to worry about it now. The dog_house is the app we’ll work on most of the time.
We can check if everything is working properly by running
poetry run python manage.py runserver
Follow the URL generated in the terminal to see the greeting page.
Outro
That’s it for today. See you next time!!
References
https://docs.djangoproject.com/en/5.2/intro/tutorial01/
https://python-poetry.org/docs/