Flask入门教程——李辉 第四章 静态文件 关键知识梳理 更新1次

第四章 静态文件 关键知识梳理

文章目录

一个概念

静态文件(static files) 和我们的模板概念相反,指的是内容不需要动态生成的文件。比如图片、CSS文件和JavaScripts脚本等。

两类图片

1.网站头像

Favicon 是显示在标签页和书签栏的网站头像。一般是像素为16X1632X3248x4864X64icopnggif格式的图片。

html 添加方法:head标签里 link标签

位置: ./static文件夹下

decos.ico

2.网页图片

为了让网页不太单调,一般都会在网页里添加一些图片。

位置:./static/images文件夹下

html添加方法:body标签里 任意位置

层叠样式表(css)

CSS(Cascading Style Sheets,层叠样式表),是一种用来为结构化文档(如 HTML 文档或 XML 应用)添加样式(字体、间距和颜色等)的计算机语言。

位置: ./static

使用: 一般需要在html文件里引入css文件,并在需要修改样式的标签里添加class属性值,最后css通过cssclass选择器

案例实现

创建静态文件夹
bash 复制代码
mkdir static
添加网站头像

decos.ico
html文件添加如下:

html 复制代码
<head>
    ...
    <link rel="icon" href="{{ url_for('static',filename='dicos.ico')}}">
</head>
添加网页图片

先创建图片文件夹

bash 复制代码
mkdir static/images

avatar.png

totoro.gif

html文件添加如下:

html 复制代码
<body>
    <h2>
        <img src="{{ url_for('static',filename='images/avatar.png')}}" alt="Avatar">
        {{ name }}'s Watchlist
    </h2>
    ...
    <img src="{{ url_for('static',filename='images/totoro.gif')}}" alt="totoro">
</body>
添加样式文件(css)

html引入css文件

html 复制代码
<head>
    ...
    <link rel="stylesheet" href="{{url_for( 'static', filename='style.css') }}" type="text/css">
</head>

html标签添加class属性

html 复制代码
    <h2>
        <img src="{{ url_for('static',filename='images/avatar.png')}}" alt="Avatar" class="avatar">
        {{ name }}'s Watchlist
    </h2>
    <ul class="movie-list">
	...
    </ul>

    <img src="{{ url_for('static',filename='images/totoro.gif')}}" alt="totoro" class="totoro">

css样式文件

css 复制代码
/* 页面整体 */
body {
    margin: auto;
    max-width: 580px;
    font-size: 14px;
    font-family: Helvetica, Arial, sans-serif;
}

/* 页脚 */
footer {
    color: #888;
    margin-top: 15px;
    text-align: center;
    padding: 10px;
}

/* 头像 */
.avatar {
    width: 40px;
}

/* 电影列表 */
.movie-list {
    list-style-type: none;
    padding: 0;
    margin-bottom: 10px;
    box-shadow: 0 2px 5px 0 rgba(0, 0, 0,0.16),
                0 2px 10px 0 rgba(0, 0, 0, 0.12);
}

.movie-list {
    padding: 12px 24px;
    border-bottom: 1px solid #ddd;
}

.movie-list li:last-child {
    border-bottom: none;
}

.movie-list li:hover {
    background-color: #f3f9fa;
}

/* 龙猫图片 */
.totoro {
    display: block;
    margin: 0 auto;
    height: 100px;
}
html直接拿
html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{ name }}'s Watchlist</title>
    <link rel="icon" href="{{ url_for('static',filename='dicos.ico')}}">
    <link rel="stylesheet" href="{{url_for( 'static', filename='style.css') }}" type="text/css">
</head>
<body>
    <h2>
        <img src="{{ url_for('static',filename='images/avatar.png')}}" alt="Avatar" class="avatar">
        {{ name }}'s Watchlist
    </h2>
    {# 使用 length 过滤器获取 movies 变量的长度 #}
    <ul class="movie-list">
        {% for movie in movies %} {# 迭代movies 变量 #}
        <li>
            {{ movie.title }} - {{ movie.year }}
            {# 等同于 movie['title'] #}
        {% endfor %} {# 不要忘记endfor 来结束for语句 #}
        </li>
    </ul>
    <img src="{{ url_for('static',filename='images/totoro.gif')}}" alt="totoro" class="totoro">
    <footer>
        <small>
            &copy; 2018 <a href="http://helloflask.com/tutorial">HelloFlask</a>
        </small>
    </footer>
</body>
</html>
htmlcss基础的直接来这里

代码里哪里不懂的直接跳转对应的网页搜索,或者直接提问AI。

html5功能参考手册

HTML5 全局属性

CSS3 参考手册

效果演示

相关推荐
v***913014 小时前
Spring boot创建时常用的依赖
java·spring boot·后端
Cosolar17 小时前
银河麒麟 / aarch64 系统:Docker + Docker Compose 完整安装教程
后端·程序员·架构
Wise玩转AI17 小时前
Day 27|智能体的 UI 与用户交互层
人工智能·python·ui·ai·chatgpt·ai智能体
星释17 小时前
Rust 练习册 100:音乐音阶生成器
开发语言·后端·rust
kaliarch17 小时前
2025年IaC生态全景与实践指南:从工具选型到多云治理
后端·云计算·自动化运维
Coder-coco17 小时前
个人健康管理|基于springboot+vue+个人健康管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·mysql·论文
b***653217 小时前
springboot整合mybatis-plus(保姆教学) 及搭建项目
spring boot·后端·mybatis
5***E68517 小时前
Spring Boot与MyBatis
spring boot·后端·mybatis
s***469817 小时前
【玩转全栈】----Django模板语法、请求与响应
数据库·python·django
x***010617 小时前
SpringSecurity+jwt实现权限认证功能
android·前端·后端