Django框架 之基础入门
django是一款MVT的框架
一、基本过程
1、创建项目:django-admin startproject 项目名称
2、编写配置文件settings.py(数据库配置、时区、后台管理中英文等)
3、创建应用:python manage.py startapp 应用名称
4、编写模型类:models.py
5、生成迁移文件: python manage.py makemigrations
6、执行迁移生成数据表: python manage.py migrate
7、后台管理:python manage.py createsuperuser
8、向admin注册模型(admin.py):admin.site.register(模型类)
9、自定义管理界面
10、运行项目:python manage.py runserver 端口
二、实现
创建应用
python manage.py startapp booktest
urls.py是后天创建的
定义模型类
from django.db import models # Create your models here. class BookInfo(models.Model): title = models.CharField(max_length=32) time = models.DateTimeField() def get_title(self): return self.title get_title.short_description = "书名" class HeroInfo(models.Model): name = models.CharField(max_length=32) book = models.ForeignKey(BookInfo, on_delete=models.CASCADE) def get_book(self): return self.book.title get_book.short_description = "书名"
说明:不需要定义主键列,在生成时会自动添加,并且值为自动增长
激活模型:编辑settings.py文件,将booktest应用加入到installed_apps中
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'booktest' ]
生成迁移文件:根据模型类生成sql语句
python manage.py makemigrations
migrations目录中
# Generated by Django 2.2.1 on 2019-06-09 01:49 from django.db import migrations, models import django.db.models.deletion class Migration(migrations.Migration): initial = True dependencies = [ ] operations = [ migrations.CreateModel( name='BookInfo', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('title', models.CharField(max_length=32)), ('time', models.DateTimeField()), ], ), migrations.CreateModel( name='HeroInfo', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('name', models.CharField(max_length=32)), ('book', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='booktest.BookInfo')), ], ), ]
执行迁移:执行sql语句生成数据表
python manage.py migrate
创建一个管理员用户
python manage.py createsuperuser,按提示输入用户名、邮箱、密码
编辑settings.py文件,设置编码、时区
LANGUAGE_CODE = 'zh-Hans' TIME_ZONE = 'Asia/Shanghai'
向admin注册booktest模块(admin.py)
from django.contrib import admin from .models import BookInfo, HeroInfo # Register your models here. class HeroInfoInline(admin.StackedInline): model = HeroInfo extra = 1 class BookInfoAdmin(admin.ModelAdmin): list_display = ['id', 'get_title', 'time'] inlines = [HeroInfoInline] admin.site.register(BookInfo, BookInfoAdmin) class HeroInfoAdmin(admin.ModelAdmin): list_display = ['name', 'get_book'] admin.site.register(HeroInfo, HeroInfoAdmin)
URLConf配置(项目urls.py)
from django.contrib import admin from django.urls import path, include, re_path urlpatterns = [ path('admin/', admin.site.urls), re_path('^', include("booktest.urls")) ]
模块urls.py编写
from django.urls import path, re_path from .views import index urlpatterns = [ re_path("^$|index$", index) ]
创建模板
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>首页</title> </head> <body> <h1>首页</h1> </body> </html>
创建视图
from django.shortcuts import render # Create your views here. def index(request): return render(request, "booktest/index.html")
运行项目
python manage.py runserver ip:port