• django重点url,视图函数,模板语言


    django重点url,视图函数,模板语言
    url
    1.django重点url无命名分组:re_path()
    2.url第一个参:url未命别名分组就不需要views中参数一定,若命别名(?P<year>[0-9]{4})/(?P<month>[0-9]{2})则必须以以
    名命为参数传值,否则获取不到值。
    3.url第三个参数:url里面传入参数,views函数里面同样要有参数,若出现参数别名和url参数重名,后面的会把前面的覆盖掉
    4.url第四个参数:url中name属性给路径起一个别名对form表单中的action=""可使用次别名引用路径引用时要用模板语言或
    直接用别名代替原路径名
    5.url映射:使用 include方法:path('myset/', include('myset.urls')),
    6.视图函数:
    render(,'')对html页面中的模板语言进行解析渲染
    在django.shortcuts中导入(但是此方法有坑,慎用)
    render_to_response("")直接引进,和上面效果相同,注:要引入render_to_response方法
    7.locals()方法(本地变量,还有其它变量):模板语言直接引用后端参数
    视图函数
    8.redirect()重定向
    模板语言
    html+逻辑控制语句
    9.Templete(模板):
    10.context(上下文):
    进入django命令行:python manage.py shell
    11.万能的句点号:可直接引用字典键值和列表值,还可以引用属性
    12.{% if %}
    13.{% for i in obj %}
    s2=[1,2,3]
    {% for foo in list %}
    <p>{{ forloop.counter }}:{{ foo }}</p>
    <p>{{ forloop.counter0 }}:{{ foo }}</p> #从零开始计数
    <p>{{ forloop.revcounter }}:{{ foo }}</p> #倒叙计数
    {% endfor %}
    14.过滤器filter:
    upper方法:使用管道符|upper内置函数把小写字母改为大写字母
    loower方法:小写
    add方法:使用管道符|add:参数,进行加法计算
    cut方法:使用管道符|cut' ' 切掉引号里面的
    date方法:时间排布
    注:传参是连接就要进行安全设置所以类似于超级链接这种直接用模板语言是不可行的
    超级链接安全设置:
    {% autoescape off %}
    {{ obj }}
    {% endautoescape %}
    safe标签:同上面的方法{% obj|safe %}
    filesize标签:数据的大小或占有量
    常用过滤器:
    add:给变量加上相应的值
    addslashes:给变量中的引号前加上斜线
    capfirst:首字母大写
    cut:从字符串中移除指定的字符
    date:格式化日期字符串
    default:如果值是False就替换成设置的默认值,否则就使用本来的值
    default_if_name:如果值是None,就替换成设置的默认值,否则就使用本来的值
    15.防止跨站请求攻击:当使用http请求返回页面的时候会出现403报错,无法进行页面渲染
    所以要使用{% csrf_token %}模板语句
    16.load标签:{% load %} 加载标签库
    17.with标签:{% with %} 用更简单的变量名代替复杂的变量名
    18.verbatim标签:{% verbatim %} 禁止render
    19.自建模板标签方法:视频53-04,必须要会
    注:@register.simple_tag装饰器在html中调用时使用{%%},并且可以传多个参数,但是不可以放在if语句下面作为条件
    @register.filter装饰器在html中调用时使用{{}},但是最多可以传入两个参数,但可以放在if下面作为条件
    settings:Installapps(app)
    文件夹名字:templatetags
    .html 文件 最上面:{% load tags %}
    大括号和小括号的区别:
    20.模板继承:避免页面重复代码太多 注:block的名字不能重复
    模板:
    <!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: 400px;
    background-color: antiquewhite;
    float: left;
    20%;
    }
    .page-body .content{
    height: 400px;
    background-color: cornflowerblue;
    float: left;
    80%;
    }
    .page-footer{
    height: 30px;
    background-color: darkcyan;
    clear: both;
    }
    </style>
    </head>
    <body>
    <div>
    <div class="page-header"></div>
    <div class="page-body"><a href=""></a>
    <div>
    <div class="menu">
    <a href="/ordered/">订单</a>
    <br>
    <a href="/shoppingcar/">购物车</a>
    </div>


    {% block content %}

    {% endblock %}


    </div>
    </div>
    <div class="page-footer"><a href=""></a></div>
    </div>
    </body>
    </html>
    继承:(1)
    {% extends "base.html" %}
    {% block content %}
    <div class="content">
    订单
    </div>
    {% endblock %}
    继承:(2)
    {% extends "base.html" %}
    {% block content %}
    <div class="content">
    购物车
    </div>
    {% endblock %}
    21.访问父模板中的块的内容,使用{{block.super}}这个标签
    模板:
    <!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: 400px;
    background-color: antiquewhite;
    float: left;
    20%;
    }
    .page-body .content{
    height: 400px;
    background-color: cornflowerblue;
    float: left;
    80%;
    }
    .page-footer{
    height: 30px;
    background-color: darkcyan;
    clear: both;
    }
    </style>
    </head>
    <body>
    <div>
    <div class="page-header"></div>
    <div class="page-body"><a href=""></a>
    <div>
    <div class="menu">
    <a href="/ordered/">订单</a>
    <br>
    <a href="/shoppingcar/">购物车</a>
    </div>


    <div class="content">

    {% block content %}
    wwwwyyyy
    {% endblock %}

    </div>


    </div>
    </div>
    <div class="page-footer"><a href=""></a></div>
    </div>
    </body>
    </html>
    继承:
    {% extends "base.html" %}
    {% block content %}
    {{ block.super }}
    <div>
    订单
    </div>
    {% endblock %}
  • 相关阅读:
    微软ASP.NET网站部署指南(4):配置项目属性
    iOS 设计模式之抽象工厂
    How can I move a MySQL database from one server to another?
    CentOS 7 上安装vim(默认未安装)
    How to resize slide dimensions without resizing any objects on the slide?
    CentOS 7.3 上安装docker
    美国留学访学(访问学者)必备信用卡
    西安理工大学税务登记证、银行账号信息
    Oracle联合多个子查询(inner join)
    linux tail
  • 原文地址:https://www.cnblogs.com/wylshkjj/p/11076770.html
Copyright © 2020-2023  润新知