Django is a powerful web framework for building dynamic, data-driven web applications. It is built on top of the Python programming language, and it provides a range of tools and features for developing and deploying web applications quickly and efficiently.
One of the key features of Django is its support for databases. Django provides a built-in object-relational mapper (ORM) that allows you to interact with a database using Python, without writing any SQL queries. This makes it easy to create, update, and delete data in a database, and to retrieve and display data in your web application.
In this tutorial, we will show you how to create a Django app and connect it to a database. We will use a MySQL database, but the steps in this tutorial will also work with other databases supported by Django, such as PostgreSQL and SQLite.
Prerequisites
Before you begin, you will need the following:
- A system running Ubuntu 22.04 or later
- Python 3 and pip installed on your system (see our tutorial on installing Python 3 and pip on Ubuntu)
- MySQL installed and configured on your system (see our tutorial on installing and configuring MySQL on Ubuntu)
Creating a Django Project
To create a Django project, we first need to install the Django package using pip
. To do this, open a terminal and run the following command:
Copy code
pip install django
This will install the latest version of Django on your system.
After Django is installed, you can use the django-admin
command to create a new Django project. To do this, run the following command:
Copy code
django-admin startproject myproject
This will create a new Django project called myproject
in the current directory. The project will contain the following files and directories:
Copy code
myproject/
├── manage.py
└── myproject/
├── __init__.py
├── asgi.py
├── settings.py
├── urls.py
└── wsgi.py
The manage.py
file is the command-line interface for managing your Django project. You can use this file to run various commands, such as starting the development server, running tests, and migrating the database.
The myproject/
directory contains the main configuration and settings for your Django project. It includes the settings.py
file, which contains the settings for your project, such as the database configuration and installed apps, and the urls.py
file, which defines the URL patterns for your project.
Creating a Django App
Now that we have created a Django project, we can create a Django app that will handle the logic and data for our web application. To do this, we will use the manage.py
file to run the startapp
command.
To create a new app called myapp
, run the following command:
Copy code
python manage.py startapp myapp
This will create a new myapp
directory in the project directory, containing the following
Connecting the Django App to the Database
Now that we have created our Django app, we need to connect it to the database. To do this, we need to configure the database settings in the settings.py
file of our Django project.
Open the settings.py
****file in a text editor and find the DATABASES
section. It should look like this:
Copy code
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
This is the default configuration for the DATABASES
setting, which uses SQLite as the database engine. We need to change this configuration to use MySQL as the database engine, and to specify the database name, user, and password that we want to use.
To do this, change the DATABASES
setting to the following:
Copy code
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mydatabase',
'USER': 'myuser',
'PASSWORD': 'mypassword',
'HOST': 'localhost',
'PORT': '3306',
}
}
Replace mydatabase
, myuser
, and mypassword
with the name of your database, the username of the database user, and the password of the database user.
After you have saved the changes to the settings.py
file, run the following command to apply the changes and create the necessary tables in the database:
Copy code
python manage.py migrate
This will create the tables in the database that are required by Django and your app.
Now that your Django app is connected to the database, you can start using the Django ORM to interact with the database. You can create, update, and delete data in the database using Python code, without writing any SQL queries.
For example, to create a new record in the database, you can use the following code:
Copy code
from myapp.models import MyModel
record = MyModel(field1='value1', field2='value2')
record.save()
This code creates a new MyModel
object with the specified values for the field1
and field2
fields, and then saves the object to the database.
To retrieve data from the database, you can use the filter
method to specify the conditions for the records that you want to retrieve. For example, to retrieve all records where the field1
field is equal.