您当前的位置: 首页 >  django
  • 0浏览

    0关注

    214博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Django(二):templates模板+静态资源目录static

不愿透露姓名の网友 发布时间:2019-09-09 20:35:12 ,浏览量:0

在这里插入图片描述

文章目录
  • 一、templates模板
    • 1.模板标签系统介绍
    • 2.调用模板的三种方法
    • 3.模板语法
      • ①取列表和字典的值
      • ②条件语句if和循环语句for
      • ③过滤器
  • 二、静态资源目录static
    • 1.静态目录的作用
    • 2.静态文件的配置
    • 3.使用静态资源文件
    • 4.静态文件的两种调用方式

一、templates模板 1.模板标签系统介绍

在做web开发,要给用户提供一个页面,页面包括静态页面+数据,两者结合起来就是完整的可视化的页面,django的模板系统支持这种功能,首先需要写一个静态页面(结构html,样式css,行为js)然后通过python的模板语法将数据渲染上去。

1.创建一个templates目录 在这里插入图片描述 2.配置模板(使用os模块的拼接) 在这里插入图片描述 配置项介绍 在这里插入图片描述

2.调用模板的三种方法

views页面

# ---------------------------------调用templates的方法一-----------------
from django.shortcuts import render
from django.http import HttpResponse

def indextmp(request):
    # render的三个参数(请求,页面,字典参数),字典参数对应页面的用{{key}}
    return render(request, 'indextmp.html', {'content': '哈哈哈哈哈'})


# ---------------------------------调用templates的方法二-----------------
from django.shortcuts import render_to_response


def a(request):
    # render_to_response两个参数(页面,字典参数),字典参数对应页面的用{{key}}
    return render_to_response('indextmp.html', {'content': '哈哈哈哈哈'})


# ---------------------------------调用templates的方法三-----------------
from django.template.loader import get_template
def b(request):
    template = get_template('indextmp.html')
    con = {'content': '哈哈哈哈哈'}
    hh = template.render(con)
    return HttpResponse(hh)

urls.py

path('indextmp/',views.indextmp),
path('a/',views.a),
path('b/',views.b),

templates模板里,创建一个indextmp.html




    
    体验模板


    11111111111111111111111111111
    {{content}}


在这里插入图片描述

3.模板语法

遵循python语法,但是需要有开始和终结符号

①取列表和字典的值
  • 取列表值在python中为list[索引值],在html中为list.索引值
  • 取字典值在python中为d[key值],在html中为d.key值

views.py

# locals()会将局部变量都转换为字典,不需要自己转换为字典
def d(request):
    name = '张三'
    age = 12
    hobby = ['唱', '跳', 'rap']
    course = {'yw': 55, 'sx': 66, 'yy': 100}
    return render(request, 'test1.html', locals())  

test1.html:




    
    测试


	姓名:{{ name }}
	年龄:{{ age }}
	爱好:{{ hobby.0 }}
	成绩:{{ course.sx }}


运行结果 在这里插入图片描述

②条件语句if和循环语句for

views.py

# 模板语法的if使用
def e(request):
    name = '张三'
    age = 33
    hobby = ['唱', '跳', 'rap']
    course = {'yw': 55, 'sx': 66, 'yy': 100}
    return render(request, 'test1.html', locals())

#—————————————————————————————————————————————
# 模板语法的for使用
def g(request):
    return render(request, 'test1.html', {'name': '张三', 'age': 12, 'hobby': ['唱', '跳', 'rap'],
                                          'course': {'yw': 55, 'sx': 66}, 'yy': 100})
#—————————————————————————————————————————————
# 模板语法的for使用,forloop负责计数次数
def h(request):
    return render(request, 'test1.html', {'name': '张三', 'age': 12, 'hobby': ['唱', '跳', 'rap'],
                                          'course': {'yw': 55, 'sx': 66}, 'yy': 100})

test1.html




    
    测试


{% if age > 15 and age < 30 %}
    我是青年
{% elif age >= 30 %}
    我是老年
{% endif %}

{% for x in hobby reversed %}
    {{ x }}
{% endfor %}

{% for x in hobby reversed %}
    {{forloop.counter}}.{{ x }}
{% endfor %}

#----------------------------------倒序且计数
{% for x in hobby reversed %}
    {% if forloop.first %}
    {{forloop.counter}}.{{ x }}
        {% elif forloop.last %}
        {{forloop.counter}}.{{ x }}
        {% else %}
        {{forloop.counter}}.{{ x }}
    {% endif %}
{% endfor %}



在这里插入图片描述

③过滤器
  • 基本格式:{{ 变量名 | 过滤器:可选参数 }}

  • 举例:

    功能举例意义小写{{ name|lower }}对name进行小写转换,前端显示为小写字符大写{{ name |upper }}对name进行大写转换,前端显示为大写字符传递后大写{{ my_list|first|upper }}对my_list的第一个元素进行大写转换截取词{{ bio|truncatewords:"30" }}将显示变量 bio 的前30个词。默认值{{ name|default:"默认值" }}当后端传入的词为false的时候,自动替换为默认值。(0, 0.0, False, "", [], () , set(), {} None)求长度{{ name|length}}展示name的长度根据字符截取长度{{ str|truncatechars:2}}只展示str的前两个字符,后边的字符用…展示安全转义{{ views_str|safe }}Django 会自动对 views.py 传到HTML文件中的标签语法进行转义,令其语义失效。加 safe 过滤器是告诉 Django 该数据是安全的,不必对其进行转义,可以让该数据语义生效。
二、静态资源目录static 1.静态目录的作用

在web开发过程当中,有一类型的文件专门存放静态资源(例如css,js,image)的静态文件。这些文件通常不被直接访问,往往是在加载页面的时候被加载,这些内容是固定的。我们一般在固定文件夹中存放这些静态资源,一般是static目录

2.静态文件的配置

①创建static目录 在这里插入图片描述

②在static目录下创建三个目录

在这里插入图片描述 ③配置静态文件setting 在这里插入图片描述

# 静态文件的配置
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR,'static'),
)

#STATICFILES_DIRS 后面可以是列表或者是元组
3.使用静态资源文件

在这里插入图片描述 views.py

def statictest(request):
    phb = [
        {'name': '吕布', 'img': 'lvbu.png'},
        {'name': '赵云', 'img': 'zy.png'},
        {'name': '关羽', 'img': 'gy.png'},
    ]
    return render(request, 'statictest.html', locals())

urls.py

path('statictest/',views.statictest),

statictest.html:




    
    测试静态文件
    


{% for one in phb %}
                
关注
打赏
1657102503
查看更多评论
0.0566s