Adam Hunter

Made with in NYC

Django REST Framework

Creating APIs with Python

6.7.21

For continuity, I would like to start this post off by mentioning despite the previous post exploring several CMS technologies, I ultimately did choose to use tried and true WordPress for a project that I hope to deliver this week. According to… the internet (I googled it) about 65% of all websites on the internet use WordPress for CMS. Another wild purported fact, 40% of all websites on the internet are actually WordPress sites. For this project, WordPress was actually perfect for me as the developer and the client so I didn’t fight it.
I wanted to write a little about Django and specifically Django REST Framework. I personally love developing with Python and Django specifically. I am not alone here on the Python/Django bandwagon – Instagram, Spotify, Pinterest, DropBox, NASA, Reddit, Udemy, Robinhood, all use Django (Most of them use React on the frontend too). One of the most powerful features Django has is its ORM, or Object Relational Mapper. In other words, you can use Python instead of SQL to create, query and modify databases. Developing the frontend you can use the Django Template Language out of the box, which is great for slipping a variable or some logic into some HTML but is ultimately a little limiting. A much more modern approach would be to develop the frontend independently with tools like React. Scalability and speed are among many more advantages this method provides.
If you are like Instagram and want to use Django as the backend of a project and connect it to a React frontend UI, one of the best ways to connect them is to have Django serve up an API. Enter the Django REST framework. Django REST framework is a toolkit for building APIs and has some impressive features such as Oauth1(& 2), serialization for aforementioned ORM data sources and a very cool web browsable API. The web browsable API is an interface where you can easily view your API instances and perform all CRUD (Create, Read, Update, Delete) functions. You can view and edit the raw JSON data or the more userfriendly HTML form. Check out the screen cap below from a recent project.


Building these API views can be accomplished with just a few lines of Python code. There are class based views and function based views. Whichever method suits your project best you will have to identify some similar information such as specifying the type of HTTP method or action you want to perform. In function based views this can be accomplished by specifying the method in the API view decorator for the function: for example @api_view([‘POST’]) . The decorator defaults to GET if left blank. Another decorator that Django REST framework provides that I think is extremely useful is @permission_classes(). With this decorator you can protect certain API views by allowing only authenticated users access. To utilize JWT (JSON Web Token) authentication, a very common approach with Django REST framework would be to use the Simple JWT library.
Speaking of JSON data and developing with Python, I’d like to mention my preference for pipenv. Pipenv describes itself as “a tool that aims to bring the best of all packaging worlds (bundler, composer, npm, cargo, yarn, etc.) to the Python world.” It does this by handling the developer dependencies with a Pipfile, think requirements.txt, and a Pipfile.lock file which is JSON and like the name suggests, handles the virtual environment. It’s very cool and familiar especially if you use npm or yarn on a regular basis.

*December 2021 addendum: Regarding that entire last paragraph, I have found pipenv to be a little limiting and have gone back to using tried and true virtualenv and requirements.txt for dependencies.

Adamadam hi

Adam