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 ...
随机推荐
- poj 1723 Soldiers【中位数】By cellur925
题目传送门 题目大意:平面上有n个士兵,给出每个士兵的坐标,求出使这些士兵站好所需要的最少移动步数.站好要求:所有士兵y相等,x相邻.即达到 (x,y), (x+1, y), (x+2,y)……的状态 ...
- Hexo瞎折腾系列(6) - 将博客同时部署到Github和Coding
前言 由于本人只是将Hexo博客同时部署到 Github 和 Coding.net ,所以这里只介绍怎么同时部署到这两个网站的pages. 之所以选择这两个网站,是因为国外用户可以访问 Github, ...
- SQL - 单引号和双引号的区别
原文转载至:SQL中的单引号和双引号有区别吗? 在标准 SQL 中,字符串使用的是单引号. 如果字符串本身也包括单引号,则使用两个单引号(注意,不是双引号,字符串中的双引号不需要另外转义). 但在其它 ...
- UWP 后台保存图片
在做UWP的时候,有一个需求,就是点击下载按钮,需要将当前页面中的Image控件中显示的图片保存下来. 既然聊到了下载图片,索性把添加图片也讲一下. 一:给Image控件添加图片 xaml代码: &l ...
- Voting CodeForces - 749C
有点意思的题 Voting CodeForces - 749C 题意:有n个人投票,每次按照第1个人~第n个人的顺序发言,如果到某个人发言时他已经被禁止发言就跳过,每个人发言时可以禁止另一个人发言或什 ...
- (028)[技术资料]et99加密狗打开函数的一个小bug
et99加密狗的打开函数,其官方vb调用申明如下:Declare Function et_OpenToken Lib "FT_ET99_API.dll" (ByRef et99ha ...
- 洛谷P2762 太空飞行计划问题(最大权闭合图)
题意 有$m$个实验,$n$中器材,每个实验需要使用一些器材 每个实验有收入,每个器材有花费 最大化收入 - 花费 Sol 最大权闭合图的经典应用 从$S$向每个实验连流量为该实验收入的边 从每个器材 ...
- Ajax的项目搭建
在搭建Ajax项目之前,首先我们的安装nginx,因为Ajax是基于nginx来运行的, 1.安装nginx 和基本的语法 http://nginx.org/ 上面的nginx的官网,下载直接安装就好 ...
- HttpServletResponse 的状态码
public static final int SC_ACCEPTED 202 public static final int SC_BAD_GATEWAY 502 public static ...
- 十个 JDBC 的最佳实践
JDBC是Java为多种关系型数据库提供的统一的访问接口,以下是我长期使用JDBC总结的十个最佳实践. 1. 使用PrearedStatement 任何一个使用过JDBC的Java程序员几乎都知道这个 ...