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
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
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django',
'USER' : 'userid',
'PASSWORD' :'password',
'HOST' : 'localhost'
}
}
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 # 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 ).
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.