Support Online
Skip to main content

Creating a Django Project

In this guide, you learn the process of connecting your Django application to a MySQL database. As a first step, you will install Django, create your database, and make the necessary settings to lay the foundations for taking your Django project live.

1. MySQL Database Preparation

1.1 Connecting to MySQL Server

First, we will connect to the MySQL server with the root user and create a database and user:

sudo mysql

With this command, you will be connected to the MySQL command line.

1.2 Database and User Creation

Create a new database (genixnode_blog_db) and a user (genix_user) with which Django will connect.


CREATE DATABASE genixnode_blog_db;
CREATE USER 'genix_user'@'localhost' IDENTIFIED WITH mysql_native_password BY 'guclu_parola';
GRANT ALL ON genixnode_blog_db.* TO 'genix_user'@'localhost';
FLUSH PRIVILEGES;

This creates a database named genixnode_blog_db and the user genix_user.


2. Editing the MySQL Configuration File

2.1 Adding Connection Information to my.cnf File

It is better for security to store database connection information in a central file, my.cnf. You can use these commands to edit this file:


sudo nano /etc/mysql/my.cnf

Add the following information to the end of the file:


[client]
database = genixnode_blog_db
user = genix_user
password = guclu_parola
default-character-set = utf8

2.2 Restarting MySQL

After editing the configuration file, restart the MySQL service:


sudo systemctl daemon-reload
sudo systemctl restart mysql

3. Creating the Django Skeleton

3.1 Creating the Application Directory and Activating the Virtual Environment

To develop your Django application, create a directory and launch your virtual environment:


mkdir genixnode_django_proje
cd genixnode_django_proje
python3 -m venv env
. env/bin/activate

3.2 Installing Django

To install Django in a virtual environment, run the following command:


pip install django

3.3 Starting the Project Skeleton

Use the following command to start your Django project:


django-admin startproject blog
cd blog

This process creates Django's default project structure.


4. Installing mysqlclient for MySQL Connection

In order for Django to communicate with the MySQL database, we need to install the mysqlclient Python library.

4.1 Installing Required Dependencies

First, install the necessary header files for MySQL:


sudo apt install libmysqlclient-dev default-libmysqlclient-dev

4.2 Installing the mysqlclient Library

Install the mysqlclient library into your virtual environment using pip:


pip install mysqlclient

5. Configuring Django Settings (settings.py)

5.1 Editing the settings.py File

Open Django's configuration file:


nano ~/my_blog_app/blog/blog/settings.py

Configure the time zone and ALLOWED_HOSTS settings as follows:


TIME_ZONE = 'Europe/Istanbul' # 🔑 Istanbul saat dilimi
ALLOWED_HOSTS = ['Sanal-Sunucu-IP-Adresi'] # 🔑 Kendi IP adresinizi buraya yazın
STATIC_ROOT = os.path.join(BASE_DIR, 'static') # 🔑 Statik dosyalar için yolu tanımlar

5.2 Configuring Database Connection

Update the DATABASES section to connect to MySQL as follows:


DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # 🔑 MySQL motorunu kullanmasını sağlar
'OPTIONS': {
'read_default_file': '/etc/mysql/my.cnf', # 🔑 Bağlantı bilgilerini my.cnf dosyasından okur
},
}
}

6. Running Initial Migrations and Creating Super Users

6.1 Running Migrations

Run the following commands to apply the changes to the database:


cd blog
python manage.py makemigrations
python manage.py migrate

6.2 Creating a Super User

Create a superuser to access the Django administration panel:


python manage.py createsuperuser

7. Configuring Firewall Settings

7.1 Configuring Firewall Settings

To open the port used by Django (8000):


sudo ufw allow 8000

7.2 Starting the Server and Testing the Connection

Test the connection by starting the server:


python manage.py runserver 0.0.0.0:8000

You should see Django's default page by going to http://Sanal-Sunucu-IP-Adresi:8000/ in your browser.


Frequently Asked Questions (FAQ)

  1. Why should I use my.cnf? This file securely stores your database connection information and prevents sensitive data from being included in the source code.

  2. Can I use another connector instead of mysqlclient? Yes, other bindings other than mysqlclient can be used, but this library is compatible with Django and fast.

  3. When should I run the createsuperuser command? Before the superuser is created, the necessary tables in the database must be created. That's why you should run the createsuperuser command after migrating.

  4. What does ALLOWED_HOSTS do? ALLOWED_HOSTS determines which IP addresses Django will accept requests from. For security, you should only include here the IP addresses you want your app to be accessible from.

  5. Can I use 127.0.0.1 to test Django on the server? Yes, but for external access you must use the server's IP address.

Result

You have now set up the basic infrastructure for your Django application and successfully configured the connection to MySQL. You can continue development on GenixNode Virtual Server!