The basic process of django self-learning (1)

Django basic file function

The
URL entry of urls.py is associated with a function (or generic class) in the corresponding views.py, and access to the URL corresponds to a function.

Views.py
processes the request sent by the user, and corresponds to it from urls.py. By rendering the web page in the templates, the displayed content, such as the user name after logging in, and the data requested by the user, can be output to the web page.

models.py is
related to database operations. You use this when storing or reading data. Of course, you don't need to use it when you don't need the database.

The forms.py
form, the user enters the data on the browser to submit, the verification of the data and the generation of the input box, of course, you don’t need to use it.


The function in views.py renders the Html template in the templates to get the dynamic content of the web page. Of course, the cache can be used to improve the speed.

The admin.py
backend can have a powerful backend with a small amount of code.

settings.py
Django settings, configuration files, such as the DEBUG switch, the location of static files, etc.

One, configure the database

Here we take the mysql database as an example to demonstrate. First, we need to import the package in __init__.py:

#使用mysql数据库前,需要导入这个pymysql包
import pymysql
pymysql.install_as_MySQLdb()

Secondly, make data changes at the Database in the settings.py file. The specific changes are: It
Insert picture description here
should be noted that if the library to be used has not been created, you need to define the library in the database first, and then fill in your own database account Password, and server IP and other configurations, so that you can successfully set the default database storage of django to the familiar mysql.

Second, create an application

A complete project should be divided into many small applications. We can use commands to create applications in the terminal environment. First, we cut into the project (the folder containing settings.py) of the files created by django, and then enter the command:

python manage.py() startapp myAPP(这里可以给自己的app起名字)

Three, activate the application

In the settings.py file of the main package, add myapp to INSTALLED_APPSInsert picture description here

Fourth, define the model under the application (create a table)

Import from django.db import models in the models.py of the created application, then create a class and define your own table data fields in the class. Here I take the class table and the student table as examples to create:

from django.db import models


# Create your models here.

class Grades(models.Model):
    gname = models.CharField(max_length=20)
    gdate = models.DateField()
    ggirlnum = models.IntegerField()
    gboynum = models.IntegerField()
    isDelete = models.BooleanField()


class Students(models.Model):
    sname = models.CharField(max_length=20)
    sgender = models.BooleanField(default=False)
    sage = models.IntegerField()
    scontend = models.CharField(max_length=20)
    isDelete = models.BooleanField(default=False)
    sgrade = models.ForeignKey("Grades", on_delete=models.CASCADE)   #定义外键的时候必须定义级联删除

Each class model must inherit models.Models, and there is no need to define a primary key when defining table data, which is automatically generated when it is created. Here we do not need to use SQL statements to define and constrain each field, we can use the method in the model to constrain. The tables defined at this stage will not be generated in the database.

Five, generate table

  • Generate the migration file
    We can still generate the migration file in the terminal environment. First, go to the project folder and enter the command (at this time, the migration file is generated in the migrations directory. At this time, the database still does not have a table, we still need Execute the migration file. ):
python manage.py makemigrations
  • Executing the migration file
    After the migration file is executed, we can use the command to execute the migration file ( equivalent to executing the SQL table creation statement ):
python manage.py migrate

Six, test data operation

(The following processes are all carried out in the python terminal environment:)

  1. Enter the command in the terminal environment at the location of the project folder:
python manage.py shell

And introduce the package:

from myAPP.models import Grades,Students
from django.utils import timezone
from datetime import *
  1. Query all data
类名.objects.all()
  1. Create data
创建类:
gra = Grades()
gra.gname = "python01"
...
...
...
gra.gboynum = 70
gra.save()    #当执行save()方法时才会将数据存入数据库当中
  1. View an object
类名.object.get(pk=2)
  1. change the data
模型对象.属性 = 新值
#修改完之后要使用 save()方法将其写入数据库
  1. delete data
模型对象.delete()
#物理删除

Guess you like

Origin blog.csdn.net/baldicoot_/article/details/107140659