How to quickly build a framework Django in the windows environment, and connecting MySql

Step one:
Open a terminal, enter the project path, enter django-admin startproject demo1 (your project name)
this is my project structure (input tree / F can view the structure.)
Here Insert Picture Description
Step two:
Open the MySql service, and here I use Navicat is a client (Django default SQLite database, we can look at their own Baidu) into the settings.py in the configuration database,

Here Insert Picture DescriptionAnd added import pymysql in _init_.py
pymysql.install_as_MySQLdb ()
The third step:
the input terminal enters demo01 directory under 01_sunk command python manage.py startapp myapp (your application name)
Here Insert Picture Descriptionto activate the application in the settings.py INSTALLED_APPS below, FIG:
Here Insert Picture Description fourth step:
interact myall following definition model database models following
the new version of the foreign key relationship is defined when the primary plus on_delete = models.CASCADE
Here Insert Picture Descriptiongenerated file migration run python manage.py makemigrations
given meaning module django. middle db.models no models this property
Here Insert Picture Descriptioninspection found models.Model wrong model class Book (models.models) changed class Book (models.Model):
continue to run, ah ha actually was being given
Here Insert Picture Descriptiona look at the causes are not id field should be set to run instead bid finally ok, tough Yeah
that's what I care deeply about the migration file, so good (this time is not yet generating a database table)
Here Insert Picture Description
continue to run python manage.py migrate ah ha, ok operating results in the following figure into the database table View

Here Insert Picture DescriptionHere Insert Picture DescriptionStep five:
Test data;
running python manage.py shell into the python terminal
introduced packages
Here Insert Picture Description
tested using, First we check the data class name .obejects.all ()
to test two bars, gain and find
Login.objects.all ( )
The result is an empty array, it is because I did not list things, all the results are empty
Here Insert Picture Description
so we add a piece of data
to create a model class instance to create a Login object
the Login = Login ()
login.name = "tom"
login.password = "123456"
login.save ()
Here Insert Picture Description
well, it's that a few steps, the code much, you can go to build a django environment pycharm above
one step ha ha ha ha

Published 34 original articles · won praise 19 · views 1442

Guess you like

Origin blog.csdn.net/qq_42236003/article/details/88596550
Recommended