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>
相关推荐
叫我DPT5 分钟前
分享一个python启动文件脚本(django示例)
数据库·python·django
_玖-幽10 分钟前
大数据分析02 基础语法差异
python·数据分析·go
coder777711 分钟前
js逆向分享
javascript·爬虫·python·算法·安全
QQ_77813297418 分钟前
从文本到视频:基于扩散模型的AI生成系统全解析(附PyTorch实现)
人工智能·pytorch·python
明月看潮生38 分钟前
青少年编程与数学 02-016 Python数据结构与算法 25课题、量子算法
python·算法·青少年编程·量子计算·编程与数学
水w40 分钟前
【Python爬虫】详细入门指南
开发语言·爬虫·python·scrapy·beautifulsoup
weixin_445054721 小时前
力扣刷题-热题100题-第35题(c++、python)
c++·python·leetcode
虾球xz2 小时前
游戏引擎学习第230天
c++·学习·游戏引擎
_x_w2 小时前
【17】数据结构之图及图的存储篇章
数据结构·python·算法·链表·排序算法·图论
pianmian12 小时前
arcgis几何与游标(1)
开发语言·python