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.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Setting Up Django with a Local MySQL Database

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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendPythondatabasemysqlDjangoWeb Development
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.