What configration dose the Database need
Engine
The engine used by the database.
Name
Name of your database
Host
Service name in Docker
Port
The local address used by the computer operating system.
User
MySQL account username
Pasword
MySQL account password
Options
A place where you can plug in an initial command like SQL Mode.
What is SQL mode?
SQL Mode is a set of rules provided by MySQL to control SQL syntax compatibility and error-handling behavior.
For example:
Should invalid data cause the insertion to fail, or be silently ignored?
Should data types be strictly checked, or automatically converted?
These behaviors are determined by the SQL Mode.
SQLite
is the default database for PyCharm
.
To import MySQL, you will have to type in command like this
from athlib import Path import pymysql pymysql.install_as_MySQLdb() from datetime import timedelta
Overall procedure (assume you are using Windows 11, using the command line and a virtual environment.)
Part 1: Install MySQL Server (If on mac os)
Step 1:
brew install mysql brew services start mysql
Part 2: Create MySQL Database & User
Step 2: Open MySQL Shell
mysql -u root -p
Step 3: Run SQL Commands
CREATE DATABASE myproject_db CHARACTER SET utf8mb4; CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword'; GRANT ALL PRIVILEGES ON myproject_db.* TO 'myuser'@'localhost'; FLUSH PRIVILEGES; EXIT;
Part 3: Set Up Django Project
Step 4: Create & Activate Virtual Environment
python -m venv venv venv\Scripts\activate
Step 5: Install Django and DRF
pip install django djangorestframework
Step 6: Start Django Project
django-admin startproject myproject cd myproject
Step 7: Start a Django App
python manage.py startapp myapp
Part 4: Install Required Python Packages
Step 8:
pip install django djangorestframework pymysql
Part 5: Start Django App
Step 9:
cd myproject python manage.py startapp myapp
Part 6: Configure Django to Use MySQL
Step 10:
In my project, in settings.py.
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'myproject_db', 'USER': 'myuser', 'PASSWORD': 'mypassword', 'HOST': 'localhost', 'PORT': '3306', 'OPTIONS': { 'init_command': "SET sql_mode='STRICT_TRANS_TABLES'", }, } }
Step 11:
In myproject/__init__.py
import pymysql pymysql.install_as_MySQLdb()
Part 7: Register App and CORS Settings
Step 12:
In settings.py
INSTALLED_APPS = [ ... 'rest_framework', 'myapp', 'corsheaders', ] MIDDLEWARE = [ 'corsheaders.middleware.CorsMiddleware', 'django.middleware.common.CommonMiddleware', ... ] ALLOWED_HOSTS = ['*'] CORS_ALLOW_ALL_ORIGINS = True
Part 8: Create a Model and Migrate
Step 13:
In myapp/models.py
from django.db import models class Student(models.Model): name = models.CharField(max_length=100)
Step 14:
python manage.py makemigrations python manage.py migrate
Part 9: Run Server on Local Network
Step 15: Start the server with IP access
python manage.py runserver 0.0.0.0:8000
Use ipconfig
in CMD to check your local IP (e.g., 192.168.1.5
)
Part 10: Frontend Fetch Testing
Step 16:
In your frontend project
export const API_BASE = "http://192.168.1.5:8000/"; fetch(API_BASE + "api/students/") .then(res => res.json()) .then(data => console.log(data)) .catch(err => console.error("Fetch failed", err));
Step 17: Browser Test
Visit: http://192.168.1.5:8000/
in your web browser.
Top comments (0)