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 ...
随机推荐
- Mac下怎么运行python3的py文件
我的Mac现在是10.14.6系统,默认自带的python版本是2.7.(怎么查看版本?打开终端,输入python即可看到版本号) 由于现在需要运行python3写的py文件,需要将自带的python ...
- AtCoder Grand Contest 005 C - Tree Restoring
题目传送门:https://agc005.contest.atcoder.jp/tasks/agc005_c 题目大意: 给定一个长度为\(N\)的整数序列\(A_i\),问能否构造一个\(N\)个节 ...
- _bzoj1029 [JSOI2007]建筑抢修【贪心 堆】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1029 经典的贪心问题,不解释. #include <cstdio> #inclu ...
- loj124 除数函数求和 1
loj124 除数函数求和 1 https://loj.ac/problem/124 $\sum_{i=1}^n(\sum_{d|i}d^k)=\sum_{i=1}^n(i^k*{\lfloor}{\ ...
- Magic Numbers CodeForces - 628D
Magic Numbers CodeForces - 628D dp函数中:pos表示当前处理到从前向后的第i位(从1开始编号),remain表示处理到当前位为止共产生了除以m的余数remain. 不 ...
- AIDL(1):简介
Android 接口定义语言 (AIDL) 1.AIDL是什么 AIDL(Android 接口定义语言)与您可能使用过的其他 IDL 类似. 您可以利用它定义客户端与服务使用进程间通信 (IPC) 进 ...
- Android 暗码表
转自: http://blog.csdn.net/jiangshide/article/details/8192834 不同手机厂商可能会隐藏或修改暗码,部份暗码要谨慎使用,因为可能令手机失去原有的功 ...
- C. Quiz 贪心 + 数学
http://codeforces.com/problemset/problem/337/C 题意是给出n个题目,那个人答对了m道,然后如果连续答对了k道,就会把分数double 求最小的分数是什么. ...
- RCC 2014 Warmup (Div. 1)
A 暴力 #include <iostream> #include<cstdio> #include<cstring> #include<algorithm& ...
- AJPFX关于Class类和Class类实例
Java程序中的各个Java类属于同一类事物,描述这类事物的Java类就是Class类.对比提问:众多的人用一个什么类表示?众多的Java类用一个什么类表示?人 PersonJava类 Cla ...