python的Web框架,Django模板标签及模板的继承

<h3 id="模板标签" data-source-line="1">模板标签
<p data-source-line="3">在传递数据的时候,会有大量的数据展示在浏览器上,而数据会是动态变化的,在html的编写中,数据也是需要动态的,而不能是写死的,如果动态展示呢。


<h4 id="给定的例子数据" data-source-line="6">给定的例子数据
<p data-source-line="7">views传递数据给html


<div class="cnblogs_code">

  django.shortcuts  
              
         students =             {:12,:,:19,:             {:22,:,:             {:25,:,:             {:43,:,:             {:88,:,:          
          render(request,,context=                      })

<h5 id="for循环" data-source-line="27"><span style="font-size: 18px;">for循环标签
<div class="cnblogs_code">

    
         
             
                 序列
                 姓名
                 年龄
                 性别
             
             
             
                  
                     
                     {{ stu.id}}
                     {{ stu.name }}
                     {{ stu.age }}
                     {{ stu.sex }}
                 
             
          
     

<p data-source-line="54">输出的结果为: css样式有过调整,请忽略样式<span class="Apple-converted-space"> 


<p data-source-line="54"><span class="Apple-converted-space">

<img src="https://www.jb51.cc/res/2019/03-02/09/5c88540af97796e9990abdbf20f2fedd.png" alt="">

<h5 id="序号排列" data-source-line="59"><span style="font-size: 18px;">forloop:序号排列
<div class="cnblogs_code">

 
         
             
                 从1开始的正向排序
                 从0开始的正向排序
                 以1结尾的倒序
                 以0结尾的倒序
             
                  
                     
                     {{ forloop.counter}}
                     {{ forloop.counter0 }}
                     {{ forloop.revcounter }}
                     {{ forloop.revcounter0 }}
                 
          
     

<p data-source-line="81">输出的结果为:<span class="Apple-converted-space"> 


<p data-source-line="81"><span class="Apple-converted-space">

<img src="https://www.jb51.cc/res/2019/03-02/09/189adade551a3bcc940e891c078fb0ed.png" alt="">

<h5 id="if判断" data-source-line="85"><span style="font-size: 18px;"><span style="font-size: 14pt;"><span style="font-size: 18pt;">if:if判断
<div class="cnblogs_code">

 
         
             
                 从1开始的正向排序
                 从0开始的正向排序
                 以1结尾的倒序
                 以0结尾的倒序
             
              
                 
                  
                     {{ forloop.counter}}
                     {{ stu.name }}
                     {{ stu.age }}
                     {{ stu.sex }}
                 
          
     

<p data-source-line="109">输出结果为:<span class="Apple-converted-space"> 


<p data-source-line="109"><span class="Apple-converted-space">

<img src="https://www.jb51.cc/res/2019/03-02/09/fa03cff0e29c8edb11d9ae283d2a8357.png" alt="">

<h5 id="简单获取url" data-source-line="112"><span style="font-size: 18px;">简单获取url
<p data-source-line="113">views简单配置点击id所展示的页面


<div class="cnblogs_code">

               HttpResponse( % pk)

<p data-source-line="118">对序号设置点击的跳转url


<div class="cnblogs_code">

  
         
             
                 从1开始的正向排序
                 从0开始的正向排序
                 以1结尾的倒序
                 以0结尾的倒序
             
                  
                 
                     
                     <{{ forloop.counter}}
                     {{ stu.name }}
                     {{ stu.age }}
                     {{ stu.sex }}
                 
          
     

<p data-source-line="141">输出结果:<span class="Apple-converted-space"> 


<p data-source-line="141"><span class="Apple-converted-space">

<img src="https://www.jb51.cc/res/2019/03-02/09/9ece91a2d7f27fe44a445ddc242b02e2.png" alt="">

<p data-source-line="141"><span class="Apple-converted-space">点击序列为1的网址显示:


<p data-source-line="145">

<img src="https://www.jb51.cc/res/2019/03-02/09/7eb2444d768c91d5c65b5c5ce4d8d1a0.png" alt="">

<h5 id="url-动态获取url返回一个命名views中path里面的name了的url的绝对路径" data-source-line="147"><span style="font-size: 18px;"><span style="font-size: 18pt;">url标签:动态获取url,返回一个命名(views中path里面的name的值)了的URL的绝对路径
<div class="cnblogs_code">

 
         
             
                 从1开始的正向排序
                 从0开始的正向排序
                 以1结尾的倒序
                 以0结尾的倒序
             
                  
                     
                       <{{ forloop.counter}}
                     {{ stu.name }}
                     {{ stu.age }}
                     {{ stu.sex }}
                 
          
     

<h5 id="for-in-empty-判断空置渲染默认的" data-source-line="172"><span style="font-size: 14pt;">for in empty 判断空置渲染默认的
<div class="cnblogs_code">

  
      
 
 {% emdfor %}

<h5 id="with类似于as" data-source-line="184"><span style="font-size: 14pt;">with:类似于as
<p data-source-line="185">第一种写法:


<div class="cnblogs_code">

   {% endwith %}

<p data-source-line="191">第二种写法


<div class="cnblogs_code">

    {% endwith %}

<h5 id="autoescape-转义开关-同过滤器的safe" data-source-line="199"><span style="font-size: 14pt;">autoescape 转义开关 同过滤器的safe
<div class="cnblogs_code">

   {% endautoescape %}

<h3 id="模板的引入" data-source-line="210">模板的引入
<p data-source-line="212">include 模板的引入,把写好的html模板添加到我们需要的html中,在html代码中添加


<div class="cnblogs_code">

 
  

<p data-source-line="218">输出结果<span class="Apple-converted-space"> 


<p data-source-line="218"><span class="Apple-converted-space">

<img src="https://www.jb51.cc/res/2019/03-02/09/cdc7f4afc95f32ee505a82f9079847ff.png" alt="">

<h3 id="模板的继承" data-source-line="225"><span style="font-size: 18pt;">模板的继承
<h5 id="extends继承标签extends代买写在html的最上面首行在此行下面的代码无效" data-source-line="227"><span style="font-size: 14pt;">extends继承标签:extends代买写在html的最上面,首行,在此行下面的代码无效。
<p data-source-line="228">被引入的内容写在block中


<div class="cnblogs_code">

     
     {% extends 'teacher/base.html'%}

<h5 id="block自定义标签在模板html中挖坑" data-source-line="234"><span style="font-size: 14pt;">block自定义标签:在模板html中挖坑
<p data-source-line="235">base模板文件.html


<div class="cnblogs_code">

    
     
      
         
         模板内的数据
      
     
      {% endblock %}

<p data-source-line="254">被引入的html文件,css,js,html代码都是此种方法写入。对应上面的模板


<div class="cnblogs_code">

       
       
      
          我是被展示的数据
      
          
     {% endblock %}

<p data-source-line="274">总结:


<pre data-source-line="276"><code class="hljs">模板标签语法:
<div class="cnblogs_code">

      
     {% tag 参数 参数 %} 例如 url

<pre data-source-line="276"><code class="hljs"> 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


注:所有源代码均实测运行过。所有源代码均已上传CSDN,请有需要的朋友自行下载。
继承APIView和ViewSetMixin;作用也与APIView基本类似,提供了身份认证、权限校验、流量管理等。ViewSet在开发接口中不经常用。
一、Django介绍Python下有许多款不同的 Web 框架。Django是重量级选手中最有代表性的一位。许多成功的网站和APP都基于Django。Django 是一个开放源代码的 Web 应用框架,由 Python 写成。Django 遵守 BSD 版权,初次发布于 2005 年 7 月, 并于 2008 年 9 月发布了第一个正式版本 1.0 。Django学习线路Django 采用了 MVT 的软件设计模式,即模型(Model),视图(View)和模板(Template)。这个MVT模式并
本文从nginx快速掌握到使用,gunicorn快速掌握到使用,实现小白快速搭建django项目,并对可能出现的报错进行了分析
uniapp微信小程序订阅消息发送服务通知
Django终端打印SQL语句 1 Setting配置: 2 默认python 使用的MysqlDB连接,Python3 支持支持pymysql 所有需要在app里面的__init__加上下面配置:
url: re_path(&#39;authors/$&#39;, views.AuthorView.as_view()), re_path(&#39;book/(?P\d+)/$&#39;, vie
前提 关于html寻找路线: template 如果在各个APP中存在, Django 会优先找全局template 文件下的html文件,如果全局下的template文件没有相关的html Djan
// GET请求request.GET // POST请求request.POST // 处理文件上传请求request.FILES // 处理如checkbox等多选 接受列表request.get
from bs4 import BeautifulSoup#kindeditordef kindeditor(request): s = &#39;&#39;&#39; &lt;li&gt;&lt;s
view.py 配置 html 配置
from django.http import JsonResponse JsonResponse 里面代码会加这一个响应头 kwargs.setdefault(&#39;content_type&#
#下面两种是基于QuerySet查询 也就是说SQL中用的jion连表的方式查询books = models.UserInfo.objects.all() print(type(books)) &gt
return HttpResponse(&quot;OK&quot;) 返回一个字符串 return redirect(&quot;/index/&quot;) 返回URL return render
from django.http import JsonResponse JsonResponse 里面代码会加这一个响应头 kwargs.setdefault(&#39;content_type&#
浏览器有一个很重要的概念——同源策略(Same-Origin Policy)。所谓同源是指,域名,协议,端口相同。不同源的客户端脚本(javascript、ActionScript)在没明确授权的情况
自动发送 &gt; 依赖jQuery文件 实例--&gt;GET请求: 手动发送 &gt; 依赖浏览器XML对象(也叫原生ajax) Ajax主要就是使用 【XmlHttpRequest】对象来完成请
#下面两种是基于QuerySet查询 也就是说SQL中用的jion连表的方式查询books = models.UserInfo.objects.all() print(type(books)) &gt
// GET请求request.GET // POST请求request.POST // 处理文件上传请求request.FILES // 处理如checkbox等多选 接受列表request.get
return HttpResponse(&quot;OK&quot;) 返回一个字符串 return redirect(&quot;/index/&quot;) 返回URL return render