url 路由系统

urlpatterns = [
# path('admin/', admin.site.urls),
path('index/', views.index),
re_path('^edit/(\w+)/$',views.edit1), # 加上 $
re_path('^edit/(\w+).html$',views.edit2), # 如果前面不加结尾符,则不能匹配此url
re_path('^edit/(?P<a2>\w+)/(?P<a1>\w+).html$',views.edit3),
# 按参数名传参 # def edit3(request,*args,**kwargs):
# 不要将(\w+)与(?P<a2>\w+)混用 path('index2/', views.index2,name='n2'),# 给url命名;在函数中通过别名反找到url
from django.urls import reverse
def index2(request):
v = reverse('n2')
return HttpResponse(v) # 通过别名反找到url,反生成url re_path('index3/(\w+)', views.index3,name='n3'), # 可随意生成(\w+)位置的值
def index3(request,*args):
v = reverse('n3',args=(123,))
# 请求url:index3/3 生成url:index3/123
# 有多少(\w+),args=(,)就写多少个参数
return HttpResponse(v) re_path('^index4/(?P<a2>\w+)/(?P<a1>\w+).html$', views.index4,name='n4'),# 可随意生成(?P<a2>\w+)位置的值
def index4(request,**kwargs):
v = reverse('n4',kwargs={'a2':666,'a1':777)) #
# 请求url:index4/4/3 生成url:/index4/666/777.html
# 有多少(?P<a2>\w+),kwargs={,}就写多少个键值对
return HttpResponse(v) re_path('^', default)
# 此规则放最后
# 什么都不输或者输错了,将会被匹配 # def default(request):return HttpResponse('url 错误')
]
若是给url命名,在templates模板中可以直接使用名字{% url 'm1' %}
path('index10/', views.index10,name='m1'),
<form method="post" action="{% url 'm1' %}"> # -><form method="post" action="/index10/">
re_path('index10/(\w+)/', views.index10,name='m2'),
<form method="post" action="{% url 'm3' 'abc' %}"> # "补位"
路由分发:
urls.py分配url到app
from django.urls import path,re_path,include
urlpatterns = [
path('app01/', include('app01.urls')),
# 只匹配开头,然后交与app01中的urls继续匹配
# 请求url为 http://127.0.0.1:8000/app01/index/
# -> 先匹配到app01 -> app01下的urls中匹配index
] 在app01.urls.py文件下匹配url
from django.urls import path,re_path
from app01 import views urlpatterns = [
path('index/', views.index),
] from django.urls import path,re_path,
re_path(r'^ip/', ([
re_path('^$', daili.display),
re_path('^page_(\d)/$', daili.display), # *args
re_path('^page_(?P<page>\d)/$', daili.display) # **kwargs
], None, None)), CBV:
path('login.html',views.Login.as_view()), from django.views import View
class Login(View):
def get(self,request):
return HttpResponse('Login.get')

模板语言

# 访问对象的属性,可以直接用.点出需要的值
{{ obj.id }}
# 访问字典用dict.key
{{ dict.key }}
# 访问元组用tuple.0 索引
{{ tuple.1 }} {% url %} 引用路由配置的地址,url的反向解析;
{% url 'name' %} {% csrf_token %}
# 防止跨站攻击,一般只在表单POST提交的时候添加;
# 其实,这里会生成一个input标签,将csrf的数据的信息以键值对的方式提交后台; # 循环
{% for item in item_list %}
<a>{{ item }}</a>
  {{ forloop.counter }}
  {{ forloop.first }}
  {{ forloop.last }}
{% endfor %} # if else
{% if ordered_warranty %}
。。。
{% else %}
...
{% endif %} 母板:{% block title %}{% endblock %}
子板:{% extends "base.html" %}
   {% block title %} 在此嵌入内容 {% endblock %}
帮助方法:
# 字母大写
{{ temp|upper}}
# 在temp的基础上加3
{{ temp|add:3 }}
# 按空格切割
{{ temp|cut:'' }}
# 以固定格式输出时间
{{ temp|date:"Y-m-d H:i:s"}}
# 内容如果为空,默认输出引号后面的内容
{{ temp|default:'空的'}}
# 将html的字符串转为html标签显示出来
{{ temp|safe }} 自定义模板语言函数
创建templatetags目录,在其下创建xxx.py文件
使用特殊方法要先导入:{% load xx %}
xxx.py
from django import template
register = template.Library() @register.filter # 只能有2个参数
def my_func(value,arg):
return value+arg
{{ name|my_func:"666"}} # 冒号后不能有空格
{%if name |my_func%}
〈h3〉真〈/h3〉
{%else%}
〈h3〉假</h3〉
{%endif%} #register.simple_tag # 可传多个参数
def my_func(value,a1,a2,a3):
return value+a1+a2+a3
{% my_func "a11" "a222" "a333" %} 小组件,可在页面多次加载,可不用{% load xx %}导入
{% include 'pub.html' %}
pub.html
<div>
<h3>特别漂亮的组件</h3>
<div class="title">标题:{{ name}}</div>
<div class="content">内容:{{ name}}</div>
</div>
index.html
<body>{% include 'pub.html' %}

Django 再次学习笔记整理的更多相关文章

  1. Deep Learning(深度学习)学习笔记整理系列之(六)

    Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...

  2. Django:学习笔记(5)——会话

    Django:学习笔记(5)——会话 配置中间件 Django中使用会话,需要配置一个中间件. 配置会话引擎 默认情况下,Django在数据库中存储sessions(使用了django.contrib ...

  3. python学习笔记整理——字典

    python学习笔记整理 数据结构--字典 无序的 {键:值} 对集合 用于查询的方法 len(d) Return the number of items in the dictionary d. 返 ...

  4. Deep Learning(深度学习)学习笔记整理系列之(五)

    Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...

  5. Deep Learning(深度学习)学习笔记整理系列之(八)

    Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...

  6. Deep Learning(深度学习)学习笔记整理系列之(七)

    Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...

  7. Deep Learning(深度学习)学习笔记整理系列之(四)

    Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...

  8. Deep Learning(深度学习)学习笔记整理系列之(三)

    Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...

  9. Deep Learning(深度学习)学习笔记整理系列之(二)

    Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...

随机推荐

  1. Luogu P4139 上帝与集合的正确用法【扩展欧拉定理】By cellur925

    题目传送门 题目中的式子很符合扩展欧拉定理的样子.(如果你还不知扩展欧拉定理,戳).对于那一堆糟心的2,我们只需要递归即可,递归边界是模数为1. 另外,本题中好像必须要用快速乘的样子...否则无法通过 ...

  2. java实训 :异常(try-catch执行顺序与自定义异常)

    关键字: try:执行可能产生异常的代码 catch:捕获异常 finally:无论是否发生异常代码总能执行 throws:声明方法可能要抛出的各种异常 throw:手动抛出自定义异常 用 try-c ...

  3. RobotFramework自动化测试框架(2)- RobotFramework语法

    RobotFramework测试用例是由四部分组成的,下面就从这四个部分简单介绍语法: 关键字表 *** Keywords *** 设置表 *** Settings *** 变量表 *** Varia ...

  4. JSP | 基础 | 新建Hello world 的三种方式

    第一种: 直接写一个 test.jsp 文件到ROOT文件目录下,内容如下,访问 “http://localhost:8080/test.jsp” <%-- Licensed to the Ap ...

  5. zoj3772Calculate the Function(矩阵+线段树)

    链接 表达式类似于斐波那契 但是多了一个变量 不能用快速幂来解 不过可以用线段树进行维护 对于每一个点够一个2*2的矩阵 1 a[i] 1  0   这个矩阵应该不陌生 类似于构造斐波那契的那个数列 ...

  6. mongoDB内置文档定义

    在最近的设计数据库时,犯了一个低级的错误,就是设置内置文档是定义了错误了,导致数据取不出,去找了很多资料都无法解决.最后看了一了一下自己设置的model文件.配置错误,所以导致数据取不出了. 数据库时 ...

  7. P1838 三子棋I

    题目描述 小a和uim喜欢互相切磋三子棋.三子棋大家都玩过是吗?就是在九宫格里面OOXX(别想歪了),谁连成3个就赢了. 由于小a比较愚蠢,uim总是让他先. 我们用9个数字表示棋盘位置: 123 4 ...

  8. 虚方法virtual详解

    虚方法virtual详解   从C#的程序编译的角度来看,它和其它一般的函数有什么区别呢?一般函数在编译时就静态地编译到了执行文件中,其相对地址在程序运行期间是不发生变化的,也就是写死了的!而虚函数在 ...

  9. windows.old文件删除

    在安装完新系统后,会发现C盘下有个windows.old文件夹,大约有个10多G,里面都是对之前系统的一些备份,用于对之前系统恢复时使用,一般一个月后会自动清理,若觉得不会再对系统进行老版本恢复时,又 ...

  10. GPU、CPU的异同

    一.概念 CPU(Center Processing Unit)即中央处理器,GPU(Graphics Processing Unit)即图形处理器. 二.CPU和GPU的相同之处 两者都有总线和外界 ...