Setting Up Django with a Local MySQL Database
This guide explains how to install MySQL, configure the Python MySQL client, set up a Django project, add an app, modify settings to connect to a local MySQL database, and run the development server to verify the installation.
Django supports multiple database back‑ends such as MySQL, PostgreSQL, SQLite, and Oracle. SQLite is built into Python, but this article focuses on using a local MySQL database with Django.
Dependencies:
1. Install MySQL locally: sudo brew install mysql After installation you need to configure MySQL yourself (refer to online guides or previous articles).
2. Install the Python MySQL client: pip install mysqlclient 3. Install a specific Django version: pip install Django==1.7 Create the first project:
1) Create the project: django-admin startproject demotestproject 2) Create an app:
cd demotestproject python manage.py startapp demoapp 3) Edit settings.py to add the app to INSTALLED_APPS: sudo vim settings.py INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'demoapp' )
4) Modify the DATABASES setting to use MySQL:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '数据库名',
'USER': 'root',
'PASSWORD': 'root',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}5) Start the project:
cd demotestproject
python manage.py runserver 9527
Open a browser and visit http://127.0.0.1:9527/ ; the page should display “it works”, confirming the setup is successful.
As the old saying goes, reading ten times is not as effective as practicing once; although the steps are simple, hands‑on practice reveals hidden pitfalls.
Long‑press the QR code below to follow for more automation learning resources.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
