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.
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.