django備忘

命令:ctrl+alt+R
設置:ctrl+alt+S


獲取硬件信息psutil模塊
1.setting路徑設置
STATICFILES_DIRS=((os.path.join(BASE_DIR, 'static')),)
2.正則路徑:
(?p<nid>\d+)
3.url傳參數
url(r^'',{'name':'alex'})
def index(req,name):
  pass
4.url路徑別名
url(r^'index',name='cc')
{% url 'cc' %}

5.render用法
方法一:
render(req,'index.html',{})

方法二:
render(req,'index.html',locals())
6.redirect頁面跳轉 重定向
redirect('/blog/blog')
7.url分發
include('')
8.{{}}列表顯示
list.2/list.x


9.模版
{% if %}
{%endif%}
{%for i in obj%}
  {{forloop.counter1/0}}計數從0或1開始
  {{forloop.revcounter}}反向打印
  {{i}}
{%endfor%}

filter:
{{obj|upper}}大寫
{{obj|lower}}小寫
{{obj|capfirst}}首字母大寫
{{obj|addslashes}}變量前加斜線
{{s6|add:5}}


安全渲染字符串到頁面
{% autoescape off%}
{{xx}}
{% endautoescape%}
或者
{{xx|safe}}

{%csrf_token%}跨站

{%verbatim %}禁止render數據顯示
  {{hello}}
{%endverbatim%}
10.自定義過濾器 simple_tag:

建立templatetags模塊
建立xx.py
from django import template
register = template.Library()#register的固定變量名,不能改變
@register.simple_tag
def func(v1):
  return v1+'world'

{%load xx%}
{%func name%} name是參數
11.html頁面繼承
{%extends ' xx.html '%}
{%block content%}
{{block.super}}

{%endblock%}

orm
建立表
class Public(models.Model):
添加數據:
方式1
Public.objects.create(字段1= ,字段= ,)
Public.objects.create(**{字段1:})
方式2
Public(字段1= ,字段= ,).save()


obj = Public()
obj.字段 =
obj.save()
查詢/查
Public.objects.filter(id = 1).delete()

obj = Public.objects.get(id = 5).name='aa'
obj = .save()

Public.objects.filter(id = 2).update(name='dd')

Public.objects.filter(name='',id=)集合對象
Public.objects.all()

Public.objects.get(name='',id=)對象

Public.objects.filter(name='',id=).values('name')只取name字段的值是列表字典序列

Public.objects.filter(name='',id=).values_list('name')
Public.objects.filter(name='',id=).distinct()剔除重複的記錄
Public.objects.filter(name='',id=).first()

Public.objects.filter(name='',id=).last()
Public.objects.filter(name='',id=).count()

Public.objects.filter(name='',id=).exists() 若是queryset包含數據 TRue,不包含 false


一對一

一對多
綁定到多的裏面,
建立方法
1.
obj_id = 1

2.
obj = obj對象

多對多

關聯
obj = Public.objects.filter(name='',id=)[0]
obj.author.add(auth1,auth2)
obj.author.add(*[auth1,auth2])
聯合惟一
class Meta: unique_together = ('field1', 'field2',)

正反項查詢

條件查詢
id__gt=2 大於

id__lt=2 小於

id__in=[1,2,3,4]等於列表任意值

title__contains='p' title包含p的都符合
icontains 忽略大小寫

id__range[1,2] 大於一 小於二

title__startwith='p' 以p開頭

關聯查詢
obj__title='python'

聚合查詢
from adjango.db.models import Avg,Min,Max,Sum
Book.objects.all().aggregate(Avg('price'))
結果:
{'price_avg':34.35}

分組聚合
按名字分組求和,
Book.objects.values('name').annotate(Sum('price'))
[{'name':'a','price_sum':Decimal(143.00)},
{'name':'b','price_sum':Decimal(143.00)}]
F,Q
from django.db.models import F,Q
Book.objects.all().update(price=F('price')+90)

Book.objects.filter(Q(id=3)|Q(id=7))

Suit-v2(demo)、Xadmin(瀏覽網站可能須要FQ)、grappellidjango-admin-bootstrapped

django-suit

鏈接數據庫
app :__init__.py
import pymysql
pymysql.install_as_MySQLdb()
ajax

$.get('url',{傳遞數據},function(data,statusTest,obj){})
$.post('url',{傳遞數據},function(data,statusTest,obj){})
$.ajax({
  url:'/ /',
  type:'POST/GET',
  data:{},
  processData:false,
  contentTYpe:'text'
  traditional:true,
  dataType;'json',
  success:function(data){
}

})


F Q:

models.Book.objects.all().update(price=F(
price)+1)

models.Book.objects.all().filter(Q(price)|Q(title));
相關文章
相關標籤/搜索