VSCODE使用Django 页面和渲染

https://code.visualstudio.com/docs/python/tutorial-django#_use-a-template-to-render-a-page

通过模板渲染页面  文件

实现步骤

1, 修改代码,hello的App名字增加到installed_apps表中。

2, hello子目录下,创建 .\templates\hello的子目录。

3,在templates\hello目录下创建 hello_there.html文件。

该文件中包含了两个占位符,name和date。这样程序只需要传递name和date两个数值就可以。

4, 修改views.py文件

引入render,更改hello_there子程序的内容。

运行结果:

关于静态文件 CSS文件

准备环境

修改web_project中urls.py文件。

模板中引用静态文件 ref to static files in template

1, hello 目录(相当于hello app)中创建 static的目录

2,static目录中创建hello子目录(与app名字相同)

3,在static\hello中创建文件: site.css

内容如下:

4, 修改hello_there.html文件 titile中增加 load static声明

5,修改hello_there.html文件body 部分

修改如下:

运行结果:

增加字体颜色渲染。

收集项目相关的所有CSS文件 

1,web_project中settings.py文件 增加一行定义收集CSS文件的位置。区分大小写。

2,运行terminal命令

目录如下:

页面继承

过程

1,在hello/templates/hello目录中创建layout.html文件。内容如下。title和content需要别的页面来提供信息。

2,在hello/static/hello/site.css文件中增加一下内容,用于渲染。

3,hello/templates/hello目录下创建home.html

内容如下:

4,同样创建about.html

5, 创建contact.html文件。

6, 修改hello文件夹下面的 urls.py

7,修改 hello文件夹下面的views.py 重写home,about,contact子程序。

结果

相关推荐

  1. Windows :VSCode安装运行Django

    2024-02-21 09:36:01       52 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-21 09:36:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-21 09:36:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-02-21 09:36:01       82 阅读
  4. Python语言-面向对象

    2024-02-21 09:36:01       91 阅读

热门阅读

  1. Go 空切片与nil切片

    2024-02-21 09:36:01       56 阅读
  2. 区块链笔记(二)

    2024-02-21 09:36:01       49 阅读
  3. 闲鱼搜索API接口

    2024-02-21 09:36:01       119 阅读
  4. Android无法获取已安装应用包名的问题

    2024-02-21 09:36:01       47 阅读
  5. svg.js

    2024-02-21 09:36:01       51 阅读
  6. linux系统消息队列的模式和介绍

    2024-02-21 09:36:01       54 阅读
  7. 浅谈数仓发展

    2024-02-21 09:36:01       49 阅读
  8. pytorch-textsummary,中文文本摘要实践

    2024-02-21 09:36:01       60 阅读
  9. 隐私保护 AI 的演变:从协议到实际实现

    2024-02-21 09:36:01       57 阅读
  10. 【Jenkins+Gitlab自动化部署配置】

    2024-02-21 09:36:01       48 阅读
  11. Node.js

    Node.js

    2024-02-21 09:36:01      42 阅读