一、django框架的创建
1)第一种方法:cmd命令行里面进行创建
a、创建一个文件夹
b、打开文件夹,按照shift键,鼠标右键,点从此处打开命令行窗口

运行django: python mannage.py runserver
2)第二种方法:pycharm里面进行创建


3、运行django框架
1、点击三角符号,运行

2、生成对话框

3、点击网址,


二、分析各个文件夹的意义
1)

noevl app/应用
novel 项目
manage.py 管理文件
templates 存放模板文件的文件夹
2)具体分析各个文件的意义
Projects
novel :APP 文件名
static
css
js
img
__init__.py :声明当前文件夹可以是一个模块
admin.py :数据库如果希望被后台看到,需要在这个文件下进行注册
apps.py
models.py :映射了数据库所创建的表
tests.py :实现某些测试驱动
viems.py :index函数返回index.html,但要templates里面去找index.html文件
templates
index.html
login.html
webapp
__init__.py
settings.py :定义了静态文件存放的路径,STATIC_URL = '/static/'。定义了数据库的类型,也就是连接所在的的数据库
urls.py :定义路由:urls(r'^$',viems.index)。==》去viems.py找到index函数
wsgi.py
三、创建应用的前的准备工作
1)修改成中文界面
LANGUAGE_CODE= 'zh-Hans'
TIME_ZONE = 'Asia/Shanghai'


2)debug模式

debug : 调试模式,开发者模式。再发布前一定要关闭
四、写入第一个程序,hello world
1)创建路由

2)创建视图

3)注意 路由 和视图 直接的关系

打开网页

五、前端文件与django的结和
前端程序:html,css,js,jquery
后端程序:python,数据库
1)html文件放入templates里面
2)视图引入 index.html文件

3)运行django程序
![570beda0abd59b73323956cc70f9985e.png]()
e)打开后台网页展示了刚刚创建的数据

2、models.py对已经存在数据库的情况下,通过命令把数据库映射成模型文件

python manage.py inspectdb > novel/models.py 生成了models.py文件
3)视频里面导入模型

测试是否获取到了数据print(NovelCopy.objects.get(id=1).novelname)
#!/usr/bin/env python #-*-coding: utf8-*- from django.shortcuts import render from django.http import HttpResponse from novel.models import NovelCopy # Create your views here. def index(request): print(NovelCopy.objects.get(id=1).novelname) # NovelCopy 数据库中的表 # objects 类的一个方法,相当于进去查看表的内容的 # get(id=1)) 找到这个id # novelname 这个id下面的一个字段 return render(request,'index.html')

七、前端随机生成数据库内容,如下

1)修改dy.html文件{% for name in movies %}
{{ name.title }} {% endfor %}
2)修改viems.py视图文件,前提是数据库连接好,并且操作数据库的models.py也写好def dy(request): moviename = dymodels.objects.all[:10] # 获取数据库的表里面的所有内容随机抽取10 names = { 'movies':"moviename", } return render(request,'dy.html',context=names)