Python Django 之 Template 模板的使用
一、模板样式
注意:
1、url
urlpatterns = {
path('admin/', admin.site.urls),
path('order/', views.order),
path('shopping_car/', views.shopping_car),
}
2、views
from django.shortcuts import render, render_to_response, redirect
from django.http import HttpResponse
import datetime
from blog import models def order(req): return render(req,"order.html") def shopping_car(req): return render(req,"shopping_car.html")
3、templates
1)order
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.page-header{
height:50px;
background-color: rebeccapurple;
}
.page-body .menu{
height: 500px;
background-color: antiquewhite;
float:left;
width: 15%;
}
.page-body .content{
height: 500px;
background-color: cornflowerblue;
float: left;
width: 85%;
}
.page-footer{
height: 30px;
background-color: darkblue;
clear: both;
}
</style>
</head>
<body>
<div>
<div class="page-header"></div>
<div class="page-body">
<div class="menu">
<a href="/order/">订单</a><br>
<a href="/shoping_car/">购物车</a>
</div>
<div class="content">订单</div> </div>
<div class="page-footer"></div>
</div>
</body>
</html>
2)shopping_car
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.page-header{
height:50px;
background-color: rebeccapurple;
}
.page-body .menu{
height: 500px;
background-color: antiquewhite;
float:left;
width: 10%;
}
.page-body .content{
height: 500px;
background-color: cornflowerblue;
float: left;
width: 90%;
}
.page-footer{
height: 30px;
background-color: darkblue;
clear: both;
}
</style>
</head>
<body>
<div>
<div class="page-header"></div>
<div class="page-body">
<div class="menu">
<a href="/order/">订单</a><br>
<a href="/shoping_car/">购物车</a>
</div>
<div class="content"> 购物车</div> </div>
<div class="page-footer"></div>
</div>
</body>
</html>
二、引入模板
注意:
1)设置模板--非公共部分使用此种方法代替
{% block content %}
{% endblock %}
2)引入模板
extends引入模板
{% block content %}包含私有部分
{% extends "base.html" %}
{% block content %}
<div class="content">订单</div>
{% endblock %}
1、url
urlpatterns = {
path('admin/', admin.site.urls),
# path('cur_time/', views.cur_time),
# # path('userInfo/',views.userInfo),
# path('login/', views.login),
# path('home/', views.home),
# path('hello/',views.hello),
path('order/', views.order),
path('shopping_car/', views.shopping_car),
}
2、views
from django.shortcuts import render, render_to_response, redirect
from django.http import HttpResponse
import datetime
from blog import models def order(req): return render(req,"order.html") def shopping_car(req): return render(req,"shopping_car.html")
3、templates
1)base
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.page-header{
height:50px;
background-color: rebeccapurple;
}
.page-body .menu{
height: 500px;
background-color: antiquewhite;
float:left;
width: 15%;
}
.page-body .content{
height: 500px;
background-color: cornflowerblue;
float: left;
width: 85%;
}
.page-footer{
height: 30px;
background-color: darkblue;
clear: both;
}
</style>
</head>
<body>
<div>
<div class="page-header"></div>
<div class="page-body">
<div class="menu">
<a href="/order/">订单</a><br>
<a href="/shoping_car/">购物车</a>
</div>
{% block content %} {% endblock %} </div>
<div class="page-footer"></div>
</div>
</body>
</html>
2)order
{% extends "base.html" %}
{% block content %}
<div class="content">订单</div>
{% endblock %}
3)shopping_car
{% extends "base.html" %}
{% block content %}
<div class="content"> 购物车</div>
{% endblock %}
三、展示效果
点击订单或者购物车,行营的url链接、右边显示的内容随之改变


Python Django 之 Template 模板的使用的更多相关文章
- Python Django 之 Template 模板语言简介
一.什么事模板语言 html+逻辑控制语句 二.模板语言的作用 帮助前端处理后端发来的数据,方便前端展示(杂糅渲染) 三.模板语言语法 1.{{变量}} 变量使用双大括号{{}} 2.万能的句点号. ...
- python Django教程 之模板渲染、循环、条件判断、常用的标签、过滤器
python3.5 manage.py runserver python Django教程 之模板渲染.循环.条件判断.常用的标签.过滤器 一.Django模板渲染模板 1. 创建一个 zqxt_tm ...
- python - django 解决 templates 模板语言语法冲突
# 在使用某个框架时发现语法与Django的模板语法冲突了,于是找到解决方案: {% verbatim %} // 被 verbatim 包裹起来的地方是不会被 django 渲染的 {% endve ...
- python框架Django中MTV框架之Template(模板/界面)
MTV框架之Template(模板/界面) 关注公众号"轻松学编程"了解更多. 1.模板目录位置 应用下 不需要注册 无法跨应用地进行复用 工程下 需要注册 settings.py ...
- Python Web框架篇:Django templates(模板)
为什么用templates? views.py视图函数是用来写Python代码的,HTML可以被直接硬编码在views.py之中.如下: import datetime def current_tim ...
- Python Django框架笔记(六):模板
(一){%%}和{{ }} {% for post in posts %} <a href=""><h2>{{ post.title }}</h2&g ...
- Python菜鸟之路:Django 路由、模板、Model(ORM)
Django路由系统 Django的路由系统让Django可以根据URI进行匹配,进而发送至特定的函数去处理用户请求.有点类似nginx的location功能. Django的路由关系分为三种:普通关 ...
- django基础2: 路由配置系统,URLconf的正则字符串参数,命名空间模式,View(视图),Request对象,Response对象,JsonResponse对象,Template模板系统
Django基础二 request request这个参数1. 封装了所有跟请求相关的数据,是一个对象 2. 目前我们学过1. request.method GET,POST ...2. reques ...
- python :Django url /views /Template 文件介绍
1,Django URL 路由系统 URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL模式以及要为该URL模式调用的视图函数之间的映射表:你就是以这种方式告诉Django ...
随机推荐
- P359 usestock2.cpp
IDE Qt Creator 4.0.3 stock.h #ifndef STOCK_H #define STOCK_H #include <string> class Stock //类 ...
- springBoot集成Redis遇到的坑(择库)源码分析为什么择库失败
提示: springboot提供了一套链接redis的api,也就是个jar包,用到的连接类叫做LettuceConnectionConfiguration,所以我们引入pom时是这样的 <de ...
- CSS3实现烟花特效 --web前端
烟花特效,比较简单,直接贴代码了…… <!DOCTYPE html><html lang="en"><head> <meta charse ...
- a gcc 4.2.4 bug(被stos指令累加后%edi作为参数的)
a gcc 4.2.4 bug(被stos指令累加后%edi作为参数的) * * Ok, now we can initialize the rest of the tty devices and c ...
- Confluence 6 如何保持我空间的整洁
如果你有很多用户在同一个空间中编辑和创建内容,你的空间将会很快的变得混乱不堪.你可以使用下面的一些步骤来避免这个的发生. 创建一系列的指南 让你的合作编辑用户知道创建页面的上级页面是什么,这样可以保证 ...
- ModelViewSet 视图集 实现接口
一.创建项目 1.创建 项目 : django-admin startprojet drf 2. 创建 两个app ------ app1 ,book python manage.py start ...
- 网络基础之 并发编程之进程,多路复用,multiprocess模块
并发 1. 背景知识 2. 什么是进程 3. 进程调度 4. 并发与并行 5 同步\异步\阻塞\非阻塞(重点) 6.multiprocess模块 7.僵尸进程与孤儿进程 1.背景知识 一操作系统的作用 ...
- Python的字典dict和set
Python内置了字典:dict的支持,dict全称dictionary: 表达式为dict{key,value} 使用键值对来存储数据 eg: 使用dict来存储姓名和分数 d = {'bob':2 ...
- 三大平衡树(Treap + Splay + SBT)总结+模板[转]
Treap树 核心是 利用随机数的二叉排序树的各种操作复杂度平均为O(lgn) Treap模板: #include <cstdio> #include <cstring> #i ...
- Liunx 更新环境时用到的命令
1.进入项目文件所在位置:cd/home/zhaorg/shelly62/apache-tomcat-7.0.5.2/bin 2.关闭服务:./shuntdown.sh或者 sh.shuntdown. ...