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. 分布式集群环境下,如何实现session共享四(部署项目测试)

    这是分布式集群环境下,如何实现session共享系列的第四篇.在上一篇:分布式集群环境下,如何实现session共享三(环境搭建)中,已经准备好了相关的环境:tomcat.nginx.redis.本篇 ...

  2. echartShow

    echartShow 基于echart和bootstrap的大屏展示 以下是已经运用的成品图片.有一些数据运用的是json文件,实际成品中运用的是真实数据,除了公司内网访问不了.  需注意的地方 图表 ...

  3. html 文本溢出显示省略号 .....

  4. Baker Vai LightOJ - 1071

    题意:类似传纸条 方法: 把他要求的操作(一个人来回),转化为两个人同时走,除了开始和结束位置只能走不同路,得到的分数和的最大值即可. 一开始想到要定义的状态,是两个人的x(行)和y(列)坐标.这样时 ...

  5. 复习Java和前端、后端框架等。

    以下便是我开始复习时做的笔记.

  6. 转 sql 查出一张表中重复的所有记录数据

    select * from DB_PATCH awhere lower(a.db_name) in (select lower(db_name) from DB_PATCH group by lowe ...

  7. Oracle apex 搭建

    参考文档 : 百度文库  <Oracle ERP APEX开发指南 > APEX HOME:  /oracle11g/product/11.2/apex/apex Oracle home: ...

  8. Suricata的输出

    不多说,直接上干货! 见官网 https://suricata.readthedocs.io/en/latest/output/index.html 总的来说,Suricata采集下来的数据输出分为: ...

  9. 外文翻译 《How we decide》多巴胺的预言 第二节

    本节阅读感言:一朝被蛇咬,十年怕井绳.我们的大脑时刻跟新着本体的预测机制. 上一节提到的喇叭,苹果汁实验可以不断的延伸扩展,在播放喇叭前用强光照射...强光照射前放置特定的图片...都可以扩展多巴胺相 ...

  10. Mac OS OneDrive 无法同步.DS_Store文件 出错

    Mac OS OneDrive 无法同步.DS_Store文件 同步出错 解决方案: 第一步:打开 terminal 窗口,到本地同步的目录下: cd "/Users/gkjglobal/G ...