Django & MySQL Database

Django

installing mysqlclient

Mysqlclient is an adopter for Python to work with MySQL database.
pip install mysqlclient
We will create one project plus2net
django-admin startproject plus2net
This will start a project plus2net. Now we will create a directory and place some files. Inside plus2net directory we have one manage.py , this we will use to run our server

change directory to plus2net
cd plus2net
Now run the server
python manage.py runserver
Once the server is running we can see the home page. Now open the URL
http://127.0.0.1:8000/
Creating an app We will create one app in the name student.
python manage.py startapp student

Connecting to MySQL

Our settings.py is located inside plus2net directory. Inside this file we will store our MySQL connection details.
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'django',
		'USER' : 'userid',
		'PASSWORD' :'password',
		'HOST' : 'localhost'
    }
}

Creating a model

Inside models.py
class student(models.Model):
    
	st_name =models.CharField(max_length=100)
	st_class=models.CharField(max_length=20)
	st_sex  =models.CharField(max_length=20)
	st_mark =models.IntegerField()
We need to add this app to our settings
INSTALLED_APPS = [
    'student.apps.StudentConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]
python manage.py migrate
python manage.py makemigrations
Once the migration is done , we will get a file inside migration folder
0001_initial.py
# Generated by Django 2.2.5 on 2020-01-13 02:20

from django.db import migrations, models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='student',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('st_name', models.CharField(max_length=100)),
                ('st_class', models.CharField(max_length=20)),
                ('st_sex', models.CharField(max_length=20)),
                ('st_mark', models.IntegerField()),
            ],
        ),
    ]
python manage.py sqlmigrate student 0001_initial
It will show us one SQL statement. Here it is
BEGIN;
--
-- Create model student
--
CREATE TABLE `student_student` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `st_name` varchar(100) NOT NULL, `st_class` varchar(20) NOT NULL, `st_sex` varchar(20) NOT NULL, `st_mark` integer NOT NULL);
COMMIT;
This will not create tables in our MySQL database , we have to do that now.
python manage.py migrate
This will run all our migrations and you will see screen like this
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions, student
Running migrations:
  Applying student.0001_initial... OK
Now this will create tables inside our database ( Check the database name you have given inside settings.py file ).

Note that this table is created with the help of Django. We have not written any SQL code to create the table. Check your PhpMyAdmin to verify the table.

Adding data to table

We can create one superuser and use the Django Admin interface to add data to our student table.

Admin to add data to student table Display data from MySQL table
Web form data inserting to MySQL table Adding multiple records to MySQL table
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer