This project will teach you how to quickly build a backend for your web app using Django which is one of the most popular frameworks for backend development. Follow the steps and test it afterwards to see that it works properly.
When you finish, you can use this project to customize it for your needs by creating your models, views (logic you want to apply on the backend with the info from your frontend), and API endpoints to communicate with your frontend.
If you also need help setting up your frontend, see my repo for that here.
If you want to learn different ways of setting your backend using Django, check out the references for more resources.
-
Create a virtual environment and activate it. If you don't know why this is necessary, check the last section of this file.
-
Install Django by typing pip install Django Also install Django REST framework by typing pip install djangorestframework You need to create a Django project by typing django-admin startproject NAME_OF_BACKEND_PROJECT Choose the name that you like. For this project, the name is backend.
-
Start the server by navigating to the NAME_OF_BACKEND_PROJECT folder and type python manage.py runserver To check that the server is running properly, in a new tab in your browser go to http://localhost:8000/ You should see a spaceship and say the installation worked successfully.
-
Migrate data to the db by navigating to your NAME_OF_BACKEND_PROJECT folder and typing python manage.py migrate Make sure you have the venv activated. You will see something like this:
Every time you want to migrate data to your database, you have to run that command. The default db is SQLite but you can change that.
- To create an API for your app, type
python manage.py startapp api
You need to create an admin user by typing
python manage.py createsuperuser
And you will need to input the username you want to create, an email, and a password.
- Create a urls.py file in api folder and paste this (check the file in this repo if you want to copy paste; the same with all the rest).
-
Go to the urls.py file in the NAME_OF_PROJECT folder and delete what you have there and paste this that will add that new urls.py file from the previous step and also add authentication so only authorized users can call the api.
-
Go to models.py in api folder, delete what you have there, and create a model by pasting this
-
Register the model created in admin.py in api folder by pasting
-
Create a serializers.py file in the api folder and paste this
A model serializer is preferred over a serializer because it’s more succinct and clear to read. ModelSerializer will automatically generate a set of fields for you based on your model and validators for the serializer.
- Install django-cors-headers library by typing
pip install django-cors-headers
See image below of settings.py file in NAME_OF_PROJECT to see how the file should look like.
The rest_framekwork.authtoken is to prevent unauthorized users from creating/deleting/editing information in our backend.
-
Go to the folder where you have your backend folder project and type python manage.py makemigrations You should see something like this Migrations for 'api': api/migrations/0001_initial.py
- Create model Article
Which states that the model Article was created successfully.
-
To push it to the database, type python manage.py migrate And you will see something like this
-
If you go to the admin panel, you will see the model Articles created and the tokens for authentication:
Click on Tokens > Add token, select a user and click save. You will see a token created for that user. Therefore, only that user with that token will be able to see the viewset and interact with the model.
- Test that your new backend works correctly by using Postman. Create a new workspace and put http://localhost:8000/api/users/ with a POST call and you should get a response with a 400 bad request error saying that username and password are required:
Go to Body in Postman, select form-data, and add username and password in KEY column and select a new username to be created and a password. You should get 201 created response with the new username created, without showing you the password value.
Now, go to your admin panel in localhost:8000 and go to Users to see the new user created.
Go to Tokens to see that a token has been created automatically for that new user.
Now, change the call to a GET and the url to http://localhost:8000/api/articles/ and you should get a 401 unauthorized error response because you didn’t provide credentials.
Go to Headers in Postman, add Authorization in KEY column and in VALUE column write Token and paste the token of the new user created. Now, you should be able to get the articles because you are using the credentials of an authorized user. You know it worked correctly if you get a 200 response.
You are all set! Customize it for your web app. Read the next section to have a better understanding of this framework.
The models.py file is where you will create the models to use in your backend. For example, if you have a sign-up for newsletters, you will need a model with the fields that the user completes when signing up.
The admin.py file is to register each new model you create. Also, you will list the fields that you want to display in the api panel.
The serializers.py file is to create serializer functions for your new models.
The views.py file is to create the view of your new model. Don't forget to import the new serializer function you created for this model in the serializers.py file.
The urls.py file in the api folder is to register endpoints so front can send data to back, and viceversa.
Don't forget to migrate the information after creating a new model by navigating to the folder that contains the manage.py file and type
python manage.py makemigrations
And then type
python manage.py migrate
If you want to rollback to a previous migration, type
python manage.py api 0009_mytoken
where api is the name of the app and 0009_mytoken is the version where you want to rollback.
Then, you need to eliminate the migrations files that are wrong (thus why I wanted to rollback), and type again
python manage.py makemigrations
And then type
python manage.py migrate
- Video tutorial called Django & ReactJS Full Stack Course [ Python Backend React Frontend ]
- Django documentation
- Serializer documentation
The virtual environment is something you create when you start a project. It's always advisable to create a virtual environment for your new project so the libraries you install there don't conflict with your other projects that might demand other versions of the libraries. For example, I am using vaderSentiment version 3.3.2 for this project, but for another project, I might need vaderSentiment version 3.2.5.
To create a virtual environment in Python 3 and using VS Code as your IDE, write this in the terminal:
py -3 -m venv name_of_project
Go to the folder that contains the virtual environment folder
And to activate the virtual environment type
name_of_project\Scripts\activate
or
source name_of_project/bin/activate
Will depend on the type of terminal you have opened (shell, bash, etc.).