Flask learning note 2

该博客介绍了Flask项目的设置。首先要设置主脚本,接着创建“templates”目录存放不同的HTML文件,其中“layout.html”用于设置网页主样式,其他页面可继承它。还可在CSS文件中定义自定义样式,静态文件需放在“static”目录,在“layout.html”中引入。此外,可使用“url_for”函数查找路由位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. Set up main script for running

from flask import Flask, render_template, url_for
# modules that are needed in this part.
# render_template can render the template we defined in the templates folder with the passed arguments for this function.
# url_for can locate the exact routes.

2. Create a directory "templates" for different html files. like "home.html", "about.html"... But most important, a "layout.html" should be created for the main style of the webset page. The other pages can extend this page. There are some blocks that can be extended in "layout.html". Like

{% block content %}{% endblock %}

3. Besides, we can define some customer style in a css file. And since in Flask, static files like JavaScript CSS need to be located in a static directory. we can put all customer information in a main.css file in this folder. And in layout.html, we should include this.

4. url_for function will find the exact location of routes for us. And in "layout.html" we can insert the following line into the <head> tag to access the main.css file mentioned above.

<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='main.css') }}">

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值