zoukankan      html  css  js  c++  java
  • django变量使用-在模板中使用视图函数中的变量

    DTL语言,即django template language

    第一次使用时,需要修改项目的setting.py文件,将其中TEMPLATES中的DIRS修改为os.path.join(BASE_DIR, 'templates'),BASE_DIR在setting.py文件中定义为BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))),表示项目所在的文件夹

    使用render时,系统会默认从setting.py文件的TEMPLATES中的DIRS所指的路径寻找渲染文件

    如果DIRS为空,而APP_DIRS为True,系统则会从项目的各个app的templates目录下去寻找渲染文件,前提是app已经添加到setting.py文件的INSTALLED_APPS中。

    templates的查找顺序:DIRS、APP_DIRS

    #项目的setting.py中templates的最初配置
    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [],
            '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',
                ],
            },
        },
    ]
    
    #INSTALLED_APPS的最初配置
    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
    ]

    template_intro_demo

    渲染模板的两种方式

    1.需要导入from django.template.loader import render_to_string,最后HttpResponse返回一个render_to_string的值

    例如html=render_to_string('index.html')  return HttpResponse(html)

    2.直接通过render返回,render返回的第一个参数为request,第二个为要渲染的文件名称,一般使用这种方式

    return render(request,'index.html')

    在模板中使用变量,有两种方法,但在html模板中必须将变量放在{{ 变量 }}中

    ①将变量存放在字典中,在render中通过context=字典的形式接收变量,然后在模板中直接通过字典的键值引用

    #视图函数
    from django.shortcuts import  render
    def index(request):
        con={'username':'ahahhah','age':25,'hobby':'basketball'}   #定义变量,变量只能为字典形式
        return render(request,'index.html',context=con) #render只能通过context接收变量
    
    #渲染文件
    ……
    <body>
       {{ username }} #直接使用字典的键获取键值,不需要使用context.键否则无法获取到值
       {{ age }}
       {{ hobby }}
    </body>
    ……

    ②变量以任何形式存在,在render中通过{'接收变量的名称':'变量名称'}的形式接收变量,然后在模板中通过接收变量的名称(如果为列表、元组、字典、对象等,需通过下标访问)引用

    #视图函数
    from django.shortcuts import  render
    def index(request):
        con=['ahahhah',25,'basketball']   #定义变量,可以是数字、字符串、列表、元组、字典、对象等
        return render(request,'index.html',{'person':con}) #render通过字典形式接收变量
    
    #渲染文件
    ……
    <body>
       {{ person.0 }} #以render中的字典的键来表示变量,并通过索引下标访问值
       {{ person.1 }}
       {{ person.2 }}
    </body>
    ……

    1.在模板中使用视图函数中的变量,需要将变量放置到{{变量名}}中

    2.如果在render中通过字典的形式接收变量并且变量为列表、元组、字典、对象等,那么想要访问变量的值,需要通过变量.属性来访问,不能通过python下标的方式访问,如访问列表的第3个元素l.2

    3.获取字典所有的key可使用d.keys,但是如果字典中有一个key的名称也为keys,那么d.keys就会获取到keys对应的键值,因此不建议在字典中定义key为字典的属性,例如keys、items、values

  • 相关阅读:
    安全编码1
    VPP tips
    VPP概述汇总
    C语言安全编码摘录
    TCP-proxy
    Scipy Lecture Notes学习笔记(一)Getting started with Python for science 1.4. Matplotlib: plotting
    Scipy Lecture Notes学习笔记(一)Getting started with Python for science 1.3. NumPy: creating and manipulating numerical data
    Scipy Lecture Notes学习笔记(一)Getting started with Python for science 1.2. The Python language
    Scipy Lecture Notes学习笔记(一)Getting started with Python for science 1.1. Python scientific computing ecosystem
    25马5跑道,求最快的五匹马的需要比赛的次数
  • 原文地址:https://www.cnblogs.com/Forever77/p/10125544.html
Copyright © 2011-2022 走看看