Django 再次学习笔记整理
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 再次学习笔记整理的更多相关文章
- Deep Learning(深度学习)学习笔记整理系列之(六)
		
Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...
 - Django:学习笔记(5)——会话
		
Django:学习笔记(5)——会话 配置中间件 Django中使用会话,需要配置一个中间件. 配置会话引擎 默认情况下,Django在数据库中存储sessions(使用了django.contrib ...
 - python学习笔记整理——字典
		
python学习笔记整理 数据结构--字典 无序的 {键:值} 对集合 用于查询的方法 len(d) Return the number of items in the dictionary d. 返 ...
 - Deep Learning(深度学习)学习笔记整理系列之(五)
		
Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...
 - Deep Learning(深度学习)学习笔记整理系列之(八)
		
Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...
 - Deep Learning(深度学习)学习笔记整理系列之(七)
		
Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...
 - Deep Learning(深度学习)学习笔记整理系列之(四)
		
Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...
 - Deep Learning(深度学习)学习笔记整理系列之(三)
		
Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...
 - Deep Learning(深度学习)学习笔记整理系列之(二)
		
Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...
 
随机推荐
- Codeforces Round #510 (Div. 2) A&B By cellur925
			
第一次CF祭== 由于太菜了只做了前两题== 因为在第一题上耗费时间太多了,我还是太菜了==. A. Benches time limit per test 1 second memory limit ...
 - Hibernate中的Query对象查询所有记录
			
映射文件,核心文件,实体类,工具类的内容都不变直接看测试方法中的代码: package com.yinfu.test; import java.util.List; import org.hibern ...
 - layui开始时间小于结束时间
			
直接上代码 // var endDate= laydate.render({ // elem: '#end_time',//选择器结束时间 // type: 'datetime', // min:&q ...
 - C++11 多线程相关的头文件
			
C++11 新标准中引入了四个头文件来支持多线程编程,他们分别是<atomic> ,<thread>,<mutex>,<condition_variable& ...
 - python之重写父类方法
			
#修改父类的方法#重写父类的方法的目的是为了给他扩展功能,父类的方法已经不能满足需求#核心思想就一句话,先调用一下你要重写的父类方法,class Coon(object): #基本类 def __in ...
 - Hdu 4725 The Shortest Path in Nya Graph (spfa)
			
题目链接: Hdu 4725 The Shortest Path in Nya Graph 题目描述: 有n个点,m条边,每经过路i需要wi元.并且每一个点都有自己所在的层.一个点都乡里的层需要花费c ...
 - Java GC基础
			
Java的垃圾回收机制负责回收无用对象占据的内存资源,但是有特殊情况:假定对象不是使用new关键字获得了一块儿“特殊”的内存区域,
 - 03.Java多线程并发库API使用2
			
1.多个线程之间共享数据的方式探讨 1.如果每个线程执行的代码相同,可以使用同一个Runnable对象,这个Runnable对象中有那个共享数据,例如,买票系统就可以这么做. 2.如果每个线程执行的代 ...
 - 【学习笔记】深入理解js原型和闭包(0)——目录
			
文章转载:https://www.cnblogs.com/wangfupeng1988/p/4001284.html 说明: 本篇文章一共16篇章,外加两篇后补的和一篇自己后来添加的学习笔记,一共19 ...
 - Java String startsWith()方法
			
描述: 这个方法有两个变体并测试如果一个字符串开头的指定索引指定的前缀或在默认情况下从字符串开始位置. 语法 此方法定义的语法如下: public boolean startsWith(String ...