Create django project
django-admin startproject project name
Create django application
django-admin startapp app name
Configuration file---in the settings.py file in the directory with the same name as the project
Register app
Database
Configuration Template Directory
Modify DIRS in TEMPLATES in settings.py
# Splicing the root directory and the created template file template is a template folder created by yourself, storing html filesof'DIRS': [os.path.join(BASE_DIR,'template')],
Write model class----write in models.py in app
from django.db import models# Define the book model classclass Book(models.Model):# Title Price Quantity Aliasname = models.CharField(max_length=30,verbose_name="book title")# Up to 5 digits of which 2 are decimalsprice = models.DecimalField(max_digits=5,decimal_places=2,verbose_name="price")# IntegerField cannot write max_lengthnum = models.IntegerField(verbose_name="number")# define metaclassclass Meta:verbose_name_plural = "Books"db_table = "book" # Specify the generated table namedef __str__(self):# Return the description of the objectreturn self.name
Migration
Generate migration files
python manage.py makemigrations
Execute migration file
python manage.py migrate
Create superuser python manage.py createsuperuser
Register model class---admin.py
Register model class in admin.pyadmin.site.register(model class)from .models import Student,Teacheradmin.site.register(Student,Teacher)
Write View
Define view function
# Display single student information String parameterdef detailStudent(request):# select * from table where id = 1; model class.objects.get(condition)# request.GET: is the request method get(): the method of obtaining data# id = request.GET.get('id')name = request.GET.get('name')# Only query data with id 1# student = Student.objects.get(id=id)student = Student.objects.get(name=name)return render(request,'detail.html',{"student":student})
Configure routing
Route distribution
To create a subrouting file
Configure the child routing file in the main routing file
Configure routes in sub-route files
Start project access
python manage.py runserver