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 ...
随机推荐
- Codeforces 984 D - XOR-pyramid
D - XOR-pyramid 思路: 区间dp dp[l][r]表示ƒ([l, r])的值 显然,状态转移方程为dp[l][r] = dp[l][r-1] ^ dp[l+1][r] 初始状态dp[i ...
- putty 显示 ubuntu的文件乱码
http://www.linuxidc.com/Linux/2012-01/52252.htm 下面几个注意点 1) Windows - Appearance - Font settings 里可以更 ...
- R quantile函数 | cut函数 | sample函数 | all函数 | scale函数 | do.call函数
取出一个数字序列中的百分位数 1. 求某一个百分比 x<-rnorm(200) quantile(x,0.9) 2. 求一系列的百分比 quantile(x,c(0.1,0.9)) quanti ...
- Watering Grass UVA - 10382
喷水装置的圆心和半径确定,就能确定左端和右端.开始时pos=0,选取左端小于pos,右端最大的更新pos. #include <iostream> #include <cstdio& ...
- java面试之谈
半个多月的找工作时间,不是在去面试路上,就是在面试中,经历了大概有近10家的面试,虽然很多家都是一回了无音讯,对自己收获还是有的,至少让自己认识到了自身基础不牢固和技术知识面的狭隘.之前从事的工作主要 ...
- ccf跳一跳
才考完,没题目,先传代码... #include<stdio.h> #include<string.h> int main() { int flag=0; int a[105] ...
- 借助python工具从word文件中抽取相关表的定义,最后组装建表语句-非常好
借助python工具从word文件中抽取表的定义,最后组装建表语句-非常好 --如有转载请以超链接的方式注明原文章出处,谢谢大家.请尊重每一位乐于分享的原创者 1.python脚本 ## -*- co ...
- 遇到后缀名为whl的库的安装方法
直接把whl文件改成zip文件,解压到site-packages里面,其中site-packages文件夹位于例如我的位置是e:/python34/lib/sit-packages即可,然后就可以用i ...
- C# 3.0 / C# 3.5 隐式(推断)类型 var
概述 你可能对隐式类型(或隐式推断类型)这个名称比较陌生,但是 var 这个关键字应该很熟悉. 在 C# 中使用 var 声明一个对象时编译器会自动根据赋值语句推断这个局部变量的类型. 赋值以后,这个 ...
- Utils--字符串的帮助类
Utils--字符串的帮助类 一个关于字符串过滤的工具类,主要时针对username和password的过滤 package com.bw.yun.utils; import java.securit ...