Django Basics
Django is a high-level Python web framework that enables the rapid development of secure and maintainable websites. It follows the Model-View-Controller (MVC) architectural pattern and encourages the use of reusable code. Django's built-in admin interface, authentication, and database models make it a popular choice for web development.
Django Rest Framework (DRF) is a powerful and flexible toolkit for building Web APIs. It is a third-party package for Django that makes it easy to build, test, and debug RESTful APIs written using the Django framework. It provides a simple and consistent interface for interacting with databases and handling request and response data.
In this chapter, we will be diving into the basics of Django and how to use it to build a web application. We will also be learning how to use Django Rest Framework to create a RESTful API for our application. This tutorial will cover topics such as creating models, views, and templates, using the Django shell, and working with the admin interface. Additionally, we will explore how to use DRF to handle authentication and permissions, and how to test and debug our API.
How to setup django on local machine
Install Python: The first step is to install Python on your machine. You can follow the instructions here to install it on your machine.
Install pip: pip is the package installer for Python. It is used to install and manage Python packages. You can check if pip is already installed by running the command
'pip --version'
in your terminal. If not, you can install it by running'python get-pip.py'
Create a virtual environment: A virtual environment is a tool that helps to keep the dependencies required by different projects separate by creating isolated python virtual environments for them. This can be done by running the command
'python -m venv myenv'
where'myenv'
can be replaced with the name of your virtual environment.
python -m venv myenv
Activate the virtual environment: Once the virtual environment is created, it needs to be activated. On Windows, you can activate the virtual environment by running
'myenv\Scripts\activate.bat'
. On Mac/Linux, you can activate the virtual environment by running'source myenv/bin/activate'
.- Windows
- Ubuntu
- macOS
```bash myenv\Scripts\activate.bat ``````bash source myenv/bin/activate ``````bash source myenv/bin/activate ```Install Django: With the virtual environment activated, you can now install Django by running the command
'pip install django'
.
pip install django
- Create a Django project: Once Django is installed, you can create a new project by running the command
'django-admin startproject myproject'
where'myproject'
can be replaced with the name of your project.
django-admin startproject myproject
- Run the development server: navigate to the project directory by running
'cd myproject'
and then run the command'python manage.py runserver'
to start the development server. You can now access the development server by going to 'http://127.0.0.1:8000/' in your browser.
cd myproject
python manage.py runserver
Now that you have set up Django on your local machine, you can start building your web application.
How to setup django rest framework on local machine
To set up Django REST framework (DRF) on your machine, you will need to have Django already installed. Here are the steps to set up DRF:
- Install the package by running
pip install djangorestframework
in your command line.
pip install djangorestframework
You might also want to install the django-cors-headers
package for making requests from React to DRF.
pip django-cors-headers
- Add 'rest_framework' to your INSTALLED_APPS in your settings.py file.
INSTALLED_APPS = [
#...
"rest_framework",
]
- Run
python manage.py makemigrations
to create the necessary database tables for DRF.
python manage.py makemigrations
- Run
python manage.py migrate
to apply the migrations.
python manage.py migrate
That's it! You should now be able to use the features of DRF in your Django project.