django 模板的创建与传参

MTV模式
M:模型
T:模板
V:视图
URL:路由器
在这里插入图片描述

模板的创建与设置

1. 创建模板文件夹

创建文件夹 名字为:templates
目录级别在 项目下
在这里插入图片描述

2. setting.py文件设置参数

DIRS 添加 创建模板路径

TEMPLATES = [
    {
    
    
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        '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',
            ],
        },
    },
]
3.在模板文件夹中添加模板

在模板文件夹中创建模板文件(.html)
创建文件 mytest.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>测试网页--模板变量</title>
</head>
<body>
    <h3>
        <!-- 视图传输过来的参数 字符串-->
        s = {
   
   { s }}
    </h3>
    <h3>
        <!-- 视图传输过来的参数  数组-->
        lst = {
   
   { lst }}
    </h3>
    <h3>
        <!-- 视图传输过来的参数  数组第0个元素-->
        lst[0] = {
   
   { lst.0 }}
    </h3>
    <h3>
        <!-- 视图传输过来的参数  字典-->
        mydic = {
   
   { mydic }}
    </h3>
    <h3>
        <!-- 视图传输过来的参数  字典元素-->
        mydic = {
   
   { mydic.name }}
    </h3>
    <h3>
        <!-- 视图传输过来的参数  函数-->
        say_hello() = {
   
   { say_hello }}
        
    </h3>
    <h3>
        <!-- 视图传输过来的参数  对象的函数-->
        dog1_say() = {
   
   { dog1.say }}
    </h3>
</body>
</html>
4.加载模板文件

方法一:

        # 1.加载模块
        from django.template import loader
        t = loader.get_template("mylogin.html")
        # 2.用模板生成html
        html = t.render({
    
    "name":"sanguo"})
        # 3.将html返回给浏览器
        return HttpResponse(html)

方法二

        from django.shortcuts import render
        return render(request, 'mylogin.html', {
    
    'name': 'hu'})

模板的传参

在这里插入图片描述

路由 urls.py

urlpatterns = [
    re_path(r"^test$", views.test_view),  # 模板变量
]

视图 views.py

# 视图传函数
def say_hello():
    return "你好"


# 视图传类
class Dog:
    def say(self):
        return "汪汪"


from django.shortcuts import render


def test_view(request):
    s = "Hello"  # 字符串
    lst = ['北京', '上海', '重庆']  # 列表
    mydic = {
    
    'name': '张三', 'age': 20}  # 字典
    dic = {
    
    's': s,  # 字符串
           'lst': lst,  # 列表
           "mydic": mydic,  # 字典
           'say_hello': say_hello,  # 函数
           'dog1': Dog()}  # 传入对象
    return render(request, "mytest.html", dic)

模板

mytest.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>测试网页--模板变量</title>
</head>
<body>
    <h3>
        <!-- 视图传输过来的参数 字符串-->
        s = {
   
   { s }}
    </h3>
    <h3>
        <!-- 视图传输过来的参数  数组-->
        lst = {
   
   { lst }}
    </h3>
    <h3>
        <!-- 视图传输过来的参数  数组第0个元素-->
        lst[0] = {
   
   { lst.0 }}
    </h3>
    <h3>
        <!-- 视图传输过来的参数  字典-->
        mydic = {
   
   { mydic }}
    </h3>
    <h3>
        <!-- 视图传输过来的参数  字典元素-->
        mydic = {
   
   { mydic.name }}
    </h3>
    <h3>
        <!-- 视图传输过来的参数  函数-->
        say_hello() = {
   
   { say_hello }}
        
    </h3>
    <h3>
        <!-- 视图传输过来的参数  对象的函数-->
        dog1_say() = {
   
   { dog1.say }}
    </h3>
</body>
</html>

收到的结果

浏览器收到的 网页

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>测试网页--模板变量</title>
</head>
<body>
    <h3>
        <!-- 视图传输过来的参数 字符串-->
        s = Hello
    </h3>
    <h3>
        <!-- 视图传输过来的参数  数组-->
        lst = [&#39;北京&#39;, &#39;上海&#39;, &#39;重庆&#39;]
    </h3>
    <h3>
        <!-- 视图传输过来的参数  数组第0个元素-->
        lst[0] = 北京
    </h3>
    <h3>
        <!-- 视图传输过来的参数  字典-->
        mydic = {&#39;name&#39;: &#39;张三&#39;, &#39;age&#39;: 20}
    </h3>
    <h3>
        <!-- 视图传输过来的参数  字典元素-->
        mydic = 张三
    </h3>
    <h3>
        <!-- 视图传输过来的参数  函数-->
        say_hello() = 你好
        
    </h3>
    <h3>
        <!-- 视图传输过来的参数  对象的函数-->
        dog1_say() = 汪汪
    </h3>
</body>
</html>

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_45875105/article/details/111866455