day02
一、昨日回顾
1、常见的http请求方法
get 获取资源
post 提交数据
put 修改数据(要确定修改的对象,获取要修改的对象的id)
delete 删除资源,(要确定删除的对象)
对应增删改查操作
head
option
2、常见的http请求状态码
200 成功
301 永久重定向
302 临时重定向
400 请求的数据有错误
404 页面找不到
401 认证失败
405 请求的方法不对,本应是get而发送了post请求,就会提示405
500 服务器内部错误
403 身份认证失败
3、 django安装
python下安装: pip install 包名
pip install django==2.2.20
4、django项目创建命令
django-admin startproject myprojectname # 创建项目
# cd myprojectname 当中
python manage.py runserver # 启动项目
创建app,打开pycharm左下的terminal,输入以下命令
python manage.py startapp app01
把app配置到项目当中
# day02.settings.py
INSTALLED_APPS = [
...
# 自建app
'app01.apps.App01Config',
]
在主路由下配置app
# day02.urls.py
"""
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('app01/', include('app01.urls')),
]
在app01下面新建urls.py,并写入以下内容
from django.urls import path
from . import views
urlpatterns = [
]
在app01下的views.py中,写一个函数视图
# app01.views.py
from django.http import HttpResponse
def index(request):
# 这里不写请求方法,默认是get
return HttpResponse('<h1>Hello Django!!</h1>')
5、django文件结构
6、django子应用
二、今日重点
模版的使用
ORM的操作
三、 今日内容
1、模版的使用
模版配置
在settings.py文件下配置模版所在目录
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
# 模版存放位置, 在根目录下,templates当中 e:\mycode\h2101A\day02
'DIRS': [os.path.join(BASE_DIR, 'templates')]
,
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
使用Django返回模版页面
1. 在templates目录下创建页面index.html, 写入以下内容
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1 style="color: red;text-align: center">Hello Django!!</h1>
</body>
</html>
2. 在子应用app下的views.py中写逻辑返回页面
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
# 这里不写请求方法,默认是get
# return HttpResponse('')
return render(request, 'index.html')
3. 书写django中的路由
app01/urls.py下写入
from django.urls import path
from . import views
urlpatterns = [
path('', views.index)
]
模板变量渲染
如果用户登录以后,用户的登录名称在网页中如何存储?要写死在网页中吗?那样的话,是不是django就没有起作用。我们应该如何去实现动态调整用户的名称。
于是乎,现在我们可以使用模版变量
来解决这个问题,页面处数据变化的位置,我们不再写死,而是通过一种特殊的语法,叫做模版变量
的形式来展示用户信息这部分数据,这个语法类似这样
{{ var }}
来简单修改一些刚才的index.html
页面
{#<h1 style="color: red;text-align: center">{{ stu_list }}</h1>#}
而视图部分,此时可以通过在视图函数内部定义相关的模版变量,来让它通过视图函数去返回 在页面中,模版变量叫做username
,那么在视图函数代码中,就需要有一个映射格式数据
,他的key
值对应*html页面中的模版变量,而value
将是这个模版变量渲染的结果
方便一些的写法还可以直接用locals
方法,将当前函数作用域下的变量及值组合为字典
如果成功的话,大家可以看到页面上可以通过视图所定义的变量进行渲染,返回 这样开发者们,也就将不再需要修改html页面,只需要在视图中,通过操作数据库
等方式将数据处理好返回即可
同时,因为html页面中更加强大,支持了模版变量这样的内容,所以我们也不在将django
中所编写html称作html
页面,而是叫做模版页面
那么render
方法,我们也可以来做一下总结,render
可以用来返回一个模板页面,并将一个字典组合成的模板变量
传递到模板页面
上,完成页面的渲染
2、模版语句循环、分支
for 标签
使用模板中使用标签语言{% for %}
和{% endfor%}
,对视图函数传递的数据集进行遍历访问,比如上面传递的字符串,列表,元祖,字典,集合这样的数据
和普通模板变量不同,模板标签使用大括号百分号的组合{% lag %}
,具有有一些特殊的功能性
模板中的标签{% for %}
与Python中的for循环类似,要记得有闭合模板标签{{ endfor }}
{# for i in items #}
{# endfor #}
1、在views.py中写一个函数视图
from django.shortcuts import render
def index(request):
# 这里不写请求方法,默认是get
# return HttpResponse('')
context = {'stu_list': [{'id': '1', 'name': '刘备'}, {'id': '2', 'name': '关羽'}]}
return render(request, 'index.html', context=context)
2、在页面当中接收context传递的内容
{% for i in items %}
这里写入循环内容
{% endfor %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table>
<tr>
<td>编号</td>
<td>名称</td>
<td>操作</td>
</tr>
{% for stu in stu_list %}
<tr>
<td>{{stu.id}}</td>
<td>{{stu.name}}</td>
<td>
<button>删除</button>
</td>
</tr>
{% endfor %}
</table>
</body>
</html>
取循环当中的索引等内容
{{forloop.counter}} # 取循环当中索引,从1开始
{{forloop.counter0}} # 取循环当中索引,从0开始
{{forloop.first}} # 取第一个
{{forloop.last}} # 取最后一个
{{forloop.revcounter}} # 取循环当中索引,从后往前取
{{forloop.revcounter0}} # 取循环当中索引,从后往前取到0
{{forloop.parentloop}} # 取循环当中父级
if语法
{% if %}
{% elif %}
{% else %}
{% endif %}
// 在html 当中使用if
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{# {{ }} 渲染context传递 的内容,获取key,可以拿到对应的值 #}
{#<h1 style="color: red;text-align: center">{{ stu_list }}</h1>#}
<table>
<tr>
<td>索引</td>
<td>编号</td>
<td>名称</td>
<td>年龄</td>
<td>操作</td>
</tr>
{# for i in items #}
{# endfor #}
{% for stu in stu_list %}
<tr>
<td>{{ forloop.revcounter }}</td>
<td>{{stu.id}}</td>
<td>{{stu.name}}</td>
{% if stu.age >= 30 %}
<td style="color: red">{{stu.age}}</td>
{% else %}
<td style="color: blue">{{stu.age}}</td>
{% endif %}
<td>
<button>删除</button>
</td>
</tr>
{% endfor %}
</table>
</body>
</html>
布尔值判断、 逻辑判断都可以做的
静态资源模版使用
在项目根目录下新建static
文件夹
# settings.py 配置
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
在页面当中引入静态资源
{% load static %}
在head标签当中写入 {% load static %}
在页面当中引入静态资源
# 在index.html当中的head标签里面
<head>
{% load static %}
<link href="{% static 'index.css' %}">
</head>
4、ORM映射
ORM映射概念
模型类编写
-
当某个子应用
APP
涉及到了数据库的使用时,要记得在settings
文件中进行配置
生成迁移文件
执行迁移