(一)正则 正则:是一种字符串的处理方式,常用来检索或者替换符合规则的内容。
内容匹配 python中的re模块 re.findall通过匹配内容的类型,长度等进行检索 结构匹配 xpath通过某个内容所在的结构检索内容实验代码
# 正则 import re string = "hello world 1 1 \n" # 原样匹配 # res = re.findall("hello",string) # print(res) # 正则 # 类型匹配 # \d 匹配数字 # res = re.findall("\d",string) # print(res) # \D 匹配非数字 # res = re.findall("\D",string) # print(res) # \w 匹配 字母 数字 下划线 # res = re.findall("\w",string) # print(res) # \W 非字母 数字 下划线 # res = re.findall("\W",string) # print(res) # \s 匹配 不可见字符 # res = re.findall("\s",string) # print(res) # \S 可见字符 # res = re.findall("\S",string) # print(res) # [] 匹配包含的字符 res = re.findall("[a-z1-9]",string) print(res) string = "151 0101 0101" # {} 长度 res = re.findall("\d",string) print(res) (二)Django中的正则路由 导入 re_path 1、路由正则 2、组匹配 可以将匹配到的内容作为参数(位置参数)进行传递 路由 res = re.findall("\d{5}",string) print(res) print("长度匹配") string = "1212 h h2" # * 匹配 0个或者多个 res = re.findall("\d*",string) print(res) # + 匹配 1个或者多个 res = re.findall("\d+",string) print(res) # ?匹配 0或者1个 res = re.findall("\d?",string) print(res) # () 组匹配 # string = "151" # res = re.findall("\d\d",string) # print(res) # # # string = "15 19 01 0w" # res = re.findall("(\d)\d",string) # print(res) # P 组名 组匹配起一个名字 string = "152 0101 0101 0" res = re.findall("(\d)\d",string) print(res) res = re.findall("(?P<xxx>\d)\d",string) print(res) # ^ 开头 res = re.findall("^1",string) print(res) # $ 结束 res = re.findall("0$",string) print(res)(二)Django中的正则路由 导入 re_path 1 路由正则
re_path("hello/\w*/",hello)2组匹配 可以将匹配到的内作为参数(位置参数)进行传递 路由
re_path("atcity/(\w*)/",atcity)视图
def atcity(request,city): return HttpResponse("在%s城市" % city)3在组名的组匹配 可以将匹配到的内容作为参数(关键字参数)进行传递,但是路由中的组名必须和视图中的参数名一样
re_path("atcity_p/(?P<year>\d{4})/(?P<city>\w+)/",atcity_p) def atcity_p(request,year,city): return HttpResponse("%s年在%s城市" % (year,city))二Django模板 (一)调用模板页面 1创建模板目录 2在setting中增加配置 3视图返回模板
from django.shortcuts import render_to_response def index(request): return render_to_response("index.html")(二)模板传值方式
from django.shortcuts import render_to_response def index(request): # 第一种 # return render_to_response("index.html",{"name":"zhangsan"}) # 第二种 # params = { # "name":"zhangsan", # "age":19 # } # return render_to_response("index.html",params) # 第三种 使用locals进行传参 name = "zhangsan" age = 12 hobby = ["唱歌","跳舞","学习"] return render_to_response("index.html",locals())(三)模板语法 1变量的使用 语法{{变量}},依然可以使用多种python的多种数据类型,和部分的方法
<p> {{ name }} {{ age }} </p> <p> 爱好:{{ hobby }} 第二个爱好: {{ hobby.1 }} </p> <p> 分数: {{ score }} python 分数: {{ score.python}} </p>2标签语法 if 判断
{% if 条件%} {% elif 条件%} {% else %} {% endif %}for
{% for 可迭代对象 %} {% endfor %} <p> <h2>for循环列表</h2> {% for one in hobby %} {{ one }} {% endfor %} </p> <p> <h2>for循环字典</h2> {% for key,value in score.items %} {{ key }}:{{ value }} {% endfor %} </p>forloop 类似于jinja2中的loop,记录了循环的状态
counter 计数 从1开始 counter0 计数,从0开始 first 是否是第一个,返回值为布尔值 last 是否为最后一个,返回值为布尔值 revcounter 倒序计数,最小为1 revcounter 倒序计数,最小为0 <h2>隔行变色</h2> {% for one in hobby %} {% if forloop.first %} <p style="background-color: red">{{ forloop.counter }}:{{ one }}</p> {% elif forloop.last %} <p style="background-color: blue">{{ forloop.counter }}:{{ one }} </p> {% else %} <p style="background-color: yellow">{{ forloop.counter }}:{{ one }} </p> {% endif %} {% endfor %}if equal判断
{% ifequal age 18 %} 18岁 {% endifequal %} {% ifequal age 19 %} 19岁 {% endifequal %}3模板过滤器 对视图传递的数据进行二次处理 语法:{{变量|方法}} {{ 变量 | 方法 :参数 }} 自带过滤器 upper lower add safe
<p> <h2>过滤器</h2> {{ name | upper | lower}} {{ age | add:10 }} {{ html | safe}} </p4继承和加载 页面中有大量的冗余代码时,可以使用继承和加载将公共的部分提取
制作base页面 提取公共的部分 独有的位置补充块 block 子页面继承base页面 使用extends继承 打卡相应的块,填入独有的内容 2加载include 提供公共的部分,编写一个html文件,在其他页面中,使用include将内容加载到指定的位置 5、静态文件处理
在manage.py 同级目录创建static目录settings中增加配置 模板中使用静态文件 第一种 <img src="/static/gtl.jpg" alt="">第二种 在html文件的头部导入static {%load static%} 使用静态文件
<img src="{% static '图片路径/图片名' %}" alt="">三Django 中的ORM (一)Django App App(application)就是应用的意思,当项目足够大的时候,我们将功能单独分成多个App进行开 发。Django 鼓励程序员进行松耦合开发,就是将项目拆分成独立的功能块进行开发,减少代码相互的 耦合(依赖)程度,保证代码的可维护性和项目结构的清晰程度。 1创建App 进入manage.py同级目录,执行命令
python manage.py startapp appname |-projectname 工程目录 |-app01 |-migrations 数据库迁移目录 |- __init__.py 子应用初始化文件 |- admin.py 站点管理 |- apps.py 子应用文件,记录了子应用的一些信息,例如子应用的名字等 |- models.py 模型文件 |- test.py 测试文件,大多情况下编写单元测试内容 |- views.py 视图文件2注册app 3使用 子应用中的views中编写视图 路由 4pycharm创建子应用
