Django学习(三)

Django的设计模式及模板层

传统的MVC(例如java)

Django的MTV

模板层:

模板加载:

代码:

views.py

python 复制代码
def test_html(request):
    #方案一
    # from django.template import loader
    # 1. 使用loader加载模板
    # t = loader.get_template('test_html.html')
    # # 将t转换成html字符串
    # html = t.render()
    # return HttpResponse(html)
    # 方案二
    from django.shortcuts import render
    return render(request, 'test_html.html')

urls.py

python 复制代码
    path('test_html', views.test_html)

templates.py

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>

视图层与模板层之间的交互:

代码:

views.py

python 复制代码
def test_html(request):
    #方案一
    # from django.template import loader
    # 1. 使用loader加载模板
    # t = loader.get_template('test_html.html')
    # # 将t转换成html字符串
    # html = t.render()
    # return HttpResponse(html)
    # 方案二
    from django.shortcuts import render
    dic = {
        'name': 'zhangsan',
        'age': 18,
        'sex': '男',
        'hobby': ['吃饭', '睡觉', '打豆豆'],
        'person': {'name': 'lisi', 'age': 20},
    }
    return render(request, 'test_html.html', dic)

templates

python 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>{{name}}今年{{ age }}性别{{ sex }}爱好{{ hobby }}</h1>
</body>
</html>

urls.py

python 复制代码
    path('test_html', views.test_html)

结果:

模板的变量和标签:

代码:

urls.py

python 复制代码
    path('test_html_param', views.test_html_param)

views.py

python 复制代码
def test_html_param(request):
    dic = {}
    dic['int'] = 88
    dic['str'] = 'xiaobai'
    dic['lst'] = ['Tom', 'Jack', 'Lily']
    dic['dict'] = {'a': 9, 'b': 8}
    dic['func'] = say_hi
    dic['classobj'] = Dog()
    return render(request, 'test_html_param.html', dic)


def say_hi():
    return 'hahah'


class Dog(object):
    def say(self):
        return 'wangwang'

templates

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h3>int是{{int }}</h3>
<h3>str是{{str }}</h3>
<h3>lst 是{{lst}}</h3>
<h3>lst是{{lst.0 }}</h3>
<h3>dict是{{dict}}</h3>
<h3>dict['a']是{{dict.a}}</h3>
<h3>function是{{func}}</h3>
<h3>class obj是{{classobj.say}}</h3>
</body>
</html>

结果:

模板标签:

代码:

urls.py

python 复制代码
    path('test_if_for', views.test_if_for)

views.py

python 复制代码
def test_if_for(request):
    dic = {}
    dic['x'] = 5
    return render(request, 'test_if_for.html', dic)

templates

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>测试if和for</title>
</head>
<body>
{% if x >= 4 %}
输入的值大于等于4
{% else %}
输入的值为小于4
{% endif %}
</body>
</html>

注意:在标签中使用变量时,直接使用变量名即可,不需要加双大括号

for标签:

urls:

python 复制代码
    path('test_if_for', views.test_if_for)

views.py:

python 复制代码
def test_if_for(request):
    dic = {}
    dic['x'] = 5
    dic['ls'] = ['a', 'b', 'c']
    return render(request, 'test_if_for.html', dic)

templates:

python 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>测试if和for</title>
</head>
<body>
<p>
{% if x >= 4 %}
输入的值大于等于4
{% else %}
输入的值为小于4
{% endif %}
<br>
{% for name in ls %}
    {%  if forloop.first %}########{% endif %}
    {{ forloop.counter0 }}{{ name }}</p>
    {% if forloop.last %}########{% endif %}
{% empty %}
没有值
{% endfor %}
</body>
</html>
相关推荐
white-persist18 分钟前
Python实例方法与Python类的构造方法全解析
开发语言·前端·python·原型模式
Java 码农43 分钟前
Centos7 maven 安装
java·python·centos·maven
007php0071 小时前
某大厂跳动面试:计算机网络相关问题解析与总结
java·开发语言·学习·计算机网络·mysql·面试·职场和发展
倔强青铜三1 小时前
苦练Python第63天:零基础玩转TOML配置读写,tomllib模块实战
人工智能·python·面试
知识分享小能手1 小时前
微信小程序入门学习教程,从入门到精通,微信小程序核心 API 详解与案例(13)
前端·javascript·学习·react.js·微信小程序·小程序·vue
递归不收敛2 小时前
吴恩达机器学习课程(PyTorch 适配)学习笔记:3.3 推荐系统全面解析
pytorch·学习·机器学习
浔川python社2 小时前
《网络爬虫技术规范与应用指南系列》(xc—3):合规实操与场景落地
python
B站计算机毕业设计之家2 小时前
智慧交通项目:Python+YOLOv8 实时交通标志系统 深度学习实战(TT100K+PySide6 源码+文档)✅
人工智能·python·深度学习·yolo·计算机视觉·智慧交通·交通标志
IT森林里的程序猿2 小时前
基于机器学习方法的网球比赛胜负趋势预测
python·机器学习·django
正牌强哥2 小时前
Futures_ML——机器学习在期货量化交易中的应用与实践
人工智能·python·机器学习·ai·交易·akshare