Django and the Django-REST Framework

Maik Luchtmeyer
2 min readNov 11, 2018
Photo by rawpixel on Unsplash

This article follows up on Part 1 of my Full Stack series, where we finished with our Python Environment.

Django

With our Python Environment setup we can now begin installing our Back-End. First install Django with (remember to open your project with PyCharm and use its terminal in order to work in your virtual environment)

$ pip install Django

Now you can create a Django skeleton in your project root and create a Django-App with

$ django-admin startproject backend
$ cd backend
$ python manage.py startapp backendapp

Now cd in your project and find the following structure

.
├── backend
│ ├── __init__.py
│ ├── __pycache__
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── backendapp
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── db.sqlite3
└── manage.py

Add your backendapp to the INSTALLED_APPS in your backend/settings.py

settings.py

To check if everything went fine, type

$ python manage.py runserver

and open http://localhost:8000. You should see the“The install worked successfully! Congratulations!”-page.

Database

For this guide we will stick with the shipping sqlite3, if you need to plug in a different DB, read here.

Now add the tables admin, auth, contenttypes, sessions to our db with

$ python manage.py migrate

and create a superuser with

$ python manage.py createsuperuser

Django-REST-Framework

With our bare Django project we can now proceed and install the Django-REST-Framework (DRF)

$ pip install djangorestframework

Add the DRF to INSTALLED_APPS

Createbackendapp/serialzer.py and add a serializer for the model django.contrib.auth.models.user

Create a corresponding viewset inside backendapp/views.py

Finally add a rest_framework.router (this will be handy if we extend our api with more endpoints) to our backend/urls.py .

To check if everything went fine, open http://localhost:8000/api/ . This should lead you to your API-Root. Now test your new API-Endpoint http://localhost:8000/api/users/, which should display the attributes of the superuser you created during the Django setup. You can also test it via curl

$ curl -H 'Accept: application/json; indent=4' http://localhost:8000/api/users/[{
"url": "http://localhost:8000/api/users/1/",
"username": "mluchtmeyer",
"email": "test@gmail.com",
"groups": []
}

This will conclude our Django/DRF setup. Since we added dependencies, we freeze them into a backend/requirements.txt

$ pip freeze > requirements.txt

We finish this part with the following structure

.
├── backend
│ ├── __init__.py
│ ├── __pycache__
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── backendapp
│ ├── __init__.py
│ ├── __pycache__
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ ├── models.py
│ ├── serializer.py
│ ├── tests.py
│ └── views.py
├── db.sqlite3
├── manage.py
└── requirements.txt

In the next part we will add a Front-End with React to our project (Link).

--

--