1. URL

URL地址说明:
使用url给视图函数传参数
在url配置中将正则部分小括号括起来。比如:
    url(r'^time/plus/(\d{1,2})/$', views.hours_ahead)
如果有多个参数则用/隔开,参数需要用分组,比如:
    url(r'^time/plus/(\d{1,2})/(\d{1,2})/$', views.hours_ahead),
给参数命名,使用正则分组的别名,比如:
    url(r'^time/plus/(?P<time1>\d{1,2})/(?P<time2>\d{1,2})/$', views.hours_ahead)
使用分组别名之后,视图函数的参数必须用分组的别名,但是位置可以不固定。
给url取别名,那么在使用此url的地方可以使用别名。比如:
    url(r'^buy/$', views.buy, name='buy'),
    url(r'^login/$', views.login, name='login'),
 

2. 反向解析

在视图函数中,反向解析url:
    from django.shortcuts import render, redirect
    from django.urls import reverse
    def buy(request):
        return redirect(reverse('index'))
        return redirect(reverse('detail', args=[2]))
        return redirect(reverse('detail', kwargs={"id": 2}))

在templates中,使用别名:
    {% url 'detail' stu.id %}
使用命名空间:
    在工程的urls.py文件中,在include时,可以指定命名空间,更加细化的划分url。比如: 
        url(r'^App/', include('App.urls', namespace='App')),
    指定命令空间后,使用反向解析时需要加上命名空间,比如:
        在视图函数中: return redirect(reverse('students:index'))
        在templates中: {% url 'students:detail' %}
 

3. 模板

在Django框架中,模板是可以帮助开发者快速生成呈现给用户页面的工具
模板的设计方式实现了我们MVT中VT的解耦,VT有着N:M的关系,一个V可以调用任意T,一个T可以供任意V使用
模板处理分为两个过程
    加载
    渲染
模板主要有两个部分
    HTML静态代码
    模板语言,动态插入的代码段(挖坑,填坑)
模板中的动态代码段除了做基本的静态填充,还可以实现一些基本的运算,转换和逻辑
模板中的变量: 视图传递给模板的数据,遵守标识符规则
    语法: {{  var }}
    如果变量不存在,则插入空字符串
    
    python manage.py shell: 进入Python环境, 且会自动导入Django配置,建议使用
 
    >>> python manage.py shell   # 进入python环境
    >>> from django import template
    >>> t = template.Template('My name is {{ name }}.')
    >>> c = template.Context({'name': 'Nige'})
    >>> print (t.render(c))
    My name is Nige.
    >>> c = template.Context({'name': 'Barry'})
    >>> print (t.render(c))
    My name is Barry.

模板中的点语法
字典查询
    >>> from django.template import Template, Context
    >>> person = {'name': 'Sally', 'age': '43'}
    >>> t = Template('{{ person.name }} is {{ person.age }} years old.')
    >>> c = Context({'person': person})
    >>> t.render(c)
    'Sally is 43 years old.'
    
属性或者方法
    >>> from django.template import Template, Context
    >>> import datetime
    >>> d = datetime.date(2017, 5, 2)
    >>> d.year
    2017
    >>> d.month
    5
    >>> d.day
    2
    >>> t = Template('The month is {{ date.month }} and the year is {{ date.year }}.')
    >>> c = Context({'date': d})
    >>> t.render(c)
    'The month is 5 and the year is 2017.'

    >>> from django.template import Template, Context
    >>> class Person(object):
    ...     def __init__(self, first_name, last_name):
    ...         self.first_name, self.last_name = first_name, last_name
    >>> t = Template('Hello, {{ person.first_name }} {{ person.last_name }}.')
    >>> c = Context({'person': Person('John', 'Smith')})
    >>> t.render(c)
    'Hello, John Smith.'

方法不能有参数。
    >>> from django.template import Template, Context
    >>> t = Template('{{ var }} -- {{ var.upper }} -- {{ var.isdigit }}')
    >>> t.render(Context({'var': 'hello'}))
    'hello -- HELLO -- False'
    >>> t.render(Context({'var': '123'}))
    '123 -- 123 -- True'

列表,使用索引,不允许负索引
    >>> from django.template import Template, Context
    >>> t = Template('Item 2 is {{ items.2 }}.')
    >>> c = Context({'items': ['apples', 'bananas', 'carrots']})
    >>> t.render(c)
    'Item 2 is carrots.'

模板中的小弊端,调用对象的方法,不能传递参数
模板中的标签
语法 {%  tag  %}
作用
    1. 加载外部传入的变量  
    2. 在输出中创建文本
    3. 控制循环或逻辑
if 语句:
    格式:
    if单分支
        {% if  表达式 %}
            语句
        {% endif  %}
    if双分支
        {%  if 表达式 %}
            语句
        {% else  %}
            语句
        {% endif %}
    if多分支
        {% if 表达式 %}
            语句
        {% elif 表达式 %}
            语句
        {% else  %}
            语句
        {% endif %}

    判断true或false
        {% if today_is_weekend %}
            <p>Welcome to the weekend!</p> 
        {% endif %}
    使用 and or not,可结合使用,and和or中 and具有更高优先权。
        {% if athlete_list and coach_list %}
            <p>Both athletes and coaches are available.</p>
        {% endif %}

        {% if not athlete_list %}
            <p>There are no athletes.</p>
        {% endif %}

        {% if athlete_list or coach_list %}
            <p>There are some athletes or some coaches.</p>
        {% endif %}

        {% if not athlete_list or coach_list %}
            <p>There are no athletes or there are some coaches.</p>
        {% endif %}
        {% if athlete_list and not coach_list %}
            <p>There are some athletes and absolutely no coaches.</p>
        {% endif %}

    使用多个相同的逻辑操作关键字也是允许的,比如:
        {% if athlete_list or coach_list or parent_list or teacher_list %}
    使用 in和not in,
        {% if "bc" in "abcdef" %}
            This appears since "bc" is a substring of "abcdef"
        {% endif %}
        {% if user not in users %}
            If users is a list, this will appear if user isn't an element of the list.
        {% endif %}
    使用 is 和 is not
        {% if somevar is True %}
            This appears if and only if somevar is True.
        {% endif %}
        {% if somevar is not None %}
            This appears if somevar isn't None.
        {% endif %}
 
 
for 语句:
    {% for 变量 in 列表 %}
        语句1 
    {% empty %}
        语句2
    {% endfor %}
    当列表为空或不存在时,执行empty之后的语句

    {{ forloop.counter }} 表示当前是第几次循环,从1数数
    {% for item in todo_list %}
        <p>{{ forloop.counter }}: {{ item }}</p>
    {%endfor %}

    {{ forloop.counter0}}表示当前是第几次循环,从0数数
    {{ forloop.revcounter}}表示当前是第几次循环,倒着数数,到1停
    {{ forloop.revcounter0}}表示当前第几次循环,倒着数,到0停
    {{ forloop.first }} 是否是第一个  布尔值
    {% for object in objects %}
        {% if forloop.first %}
            <li class="first">
        {% else %}
            <li>
        {% endif %}
        {{ object }}</li>
    {% endfor %}

    {{ forloop.last }} 是否是最后一个 布尔值
    {% for link in links %}
        {{ link }}{% if not forloop.last %} | {% endif %}
    {% endfor %}

    forloop.parentloop
    {% for country in countries %}
      <table>
          {% for city in country.city_list %}
          <tr>
              <td>Country #{{ forloop.parentloop.counter }}</td>
              <td>City #{{ forloop.counter }}</td>
              <td>{{ city }}</td>
          </tr>
          {% endfor %}
      </table>
     {% endfor %}

注释:
    单行注释
    {#  被注释掉的内容  #}
    多行注释
    {% comment %}
        内容
    {% endcomment %}

过滤器: 
    {{ var|过滤器 }}
    作用:在变量显示前修改

    add {{ value|add:2 }}
    没有减法过滤器,但是加法里可以加负数
        {{ value|add:-2 }}
    lower 
        {{ name|lower }}
    upper
        {{ my_list|first|upper }}
    截断:
        {{ bio|truncatechars:30 }}
    过滤器可以传递参数,参数需要使用引号引起来
    比如join: {{ students|join:'=' }}

    默认值:default,格式 {{var|default:value}}
    如果变量没有被提供或者为False,空,会使用默认值
    根据指定格式转换日期为字符串,处理时间的
    就是针对date进行的转换
        {{  dateVal | date:'y-m-d' }}

HTML转义
    将接收到的数据当成普通字符串处理还是当成HTML代码来渲染的一个问题
    渲染成html:{{ code|safe }}
    关闭自动转义
    {% autoescape off%}
        code
    {% endautoescape %}
    打开自动转义转义
    {% autoescape on%}
        code
    {% endautoescape %}

模板继承
    block:挖坑
        {% block XXX%}
            code
        {% endblock %}
    extends 继承,写在开头位置
        {% extends '父模板路径' %}
    include: 加载模板进行渲染
         {% include '模板文件' %}
    
 

Django (二) url 和 模板的更多相关文章

  1. python django基础二URL路由系统

    URL配置 基本格式 from django.conf.urls import url #循环urlpatterns,找到对应的函数执行,匹配上一个路径就找到对应的函数执行,就不再往下循环了,并给函数 ...

  2. 3/19 Django框架 url路由配置及模板渲染

    3/19 Django框架 url路由配置及模板渲染 1.路由分配 URL(Uniform Resoure Locato):统一资源定位符是对可以从互联网上得到的资源的位置和访问方法的一种简洁的表示, ...

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

    django重点url,视图函数,模板语言url 1.django重点url无命名分组:re_path() 2.url第一个参:url未命别名分组就不需要views中参数一定,若命别名(?P<y ...

  4. Python第十三天 django 1.6 导入模板 定义数据模型 访问数据库 GET和POST方法 SimpleCMDB项目 urllib模块 urllib2模块 httplib模块 django和web服务器整合 wsgi模块 gunicorn模块

    Python第十三天   django 1.6   导入模板   定义数据模型   访问数据库   GET和POST方法    SimpleCMDB项目   urllib模块   urllib2模块 ...

  5. django中url路由配置及渲染方式

    今天我们学习如何配置url.如何传参.如何命名.以及渲染的方式,内容大致有以下几个方面. 创建视图函数并访问 创建app django中url规则 捕获参数 路径转换器 正则表达式 额外参数 渲染方式 ...

  6. Django的URL路由系统

    一. URL配置 URL配置就像Django所支撑网站的目录.它的本质是URL与要为该URL调用的视图之间的映射表.你就是以这种方式告诉Django,对于哪个URL调用的这段代码. 基本格式 from ...

  7. Django之URL路由系统

    一 URL配置 Django 1.11版本 URLConf官方文档 URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL与要为该URL调用的视图函数之间的映射表.你就是以这 ...

  8. Django之URL控制器(路由层)

    url(r'^articles/(?P<year>[0-9]{4})/$', views.year_archive), 一.视图层路由配置系统 URL配置(URLconf)就像Django ...

  9. Django 02 url路由配置及渲染方式

    Django 02 url路由配置及渲染方式 一.URL #URL #(Uniform Resoure Locator) 统一资源定位符:对可以从互联网上得到的资源的位置和访问方法的一种简洁的表示,是 ...

随机推荐

  1. Python序列——Unicode

    Unicode是什么 Python中的Unicode 编码与解码 在应用中使用Unicode的建议 1. Unicode是什么 Unicode是对字符进行编码的一种标准.而utf8或者utf-8是根据 ...

  2. BestCoder3 1002 BestCoder Sequence(hdu 4908) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4908 题目意思:给出 一个从1~N 的排列你和指定这个排列中的一个中位数m,从这个排列中找出长度为奇数 ...

  3. <编程>比较两种素数表生成算法+计算程序运行时间+通过CMD重定向测试程序

    最近学习加密算法,需要生成素数表,一开始使用简单的循环,从2开始判断.代码如下: #include<iostream> #include<cstdio> #include< ...

  4. [ZJOI 2012] 网络

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2816 [算法] 对每种颜色的边建一棵LCT , 维护联通性即可 时间复杂度 : O( ...

  5. CopyOnWrite 策略

    CopyOnWrite 是用于解决并发读写的一种策略,在Write的时候对共享变量进行Copy,在副本上进行更新,再把更新好的副本原子性地替换原来的共享变量.写入时复制是一种优化策略,多个调用者同时访 ...

  6. bzoj 3771 Triple —— FFT

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3771 令多项式的系数是方案数,次数是值: 设 a(x) 为一个物品的多项式,即 a[w[i] ...

  7. JAVA 需要理解的重点 二

    1.常用设计模式 单例模式:懒汉式.饿汉式.双重校验锁.静态加载,内部类加载.枚举类加载.保证一个类仅有一个实例,并提供一个访问它的全局访问点. 代理模式:动态代理和静态代理,什么时候使用动态代理. ...

  8. centos7升级最新内核

    由于最近在测试ceph 的straw2算法,但是要使用straw2需要最新为4.1.0的内核,因此决定将虚机内核升级最新4.11.4. 步骤1.检查本机内核版本 #uname -sr 3.10.0-5 ...

  9. SSAS GUID 添加 行计数,非重复计数 等 遇到的莫名其妙的问题

    在基于某个GUID 进行非重复性计数时 需要对GUID 转换类型,如:CAST(ColumnName as varchar(36)) 可参考:http://stackoverflow.com/ques ...

  10. tetrahedron

    题意: 求解一个四面体的内切球. 解法: 首先假设内切球球心为$(x0,x1,x2)$,可以用$r = \frac{3V}{S_1+S_2+S_3+S_4}$得出半径, 这样对于四个平面列出三个方程, ...