s14day21
上節内容回顧:
1、請求周期
url> 路由 > 函數或類 > 傳回字元串或者模闆語言?
Form表單送出:
送出 -> url > 函數或類中的方法
- ....
HttpResponse('....')
render(request,'index.html')
redirect('/index/')
使用者 < < 傳回字元串
(當接受到redirect時)自動發起另外一個請求
--> url .....
Ajax:
$.ajax({
url: '/index/',
data: {'k': 'v', 'list': [1,2,3,4], 'k3': JSON.stringfy({'k1': 'v'}))}, $(form對象).serilize()
type: 'POST',
dataType: 'JSON':
traditional: true,
success:function(d){
location.reload() # 重新整理
location.href = "某個位址" # 跳轉
}
})
送出 -> url -> 函數或類中的方法
HttpResponse('{}')
render(request, 'index.html', {'name': 'v1'})
<h1>` name `</h1> -->
<h1>v1</h1>
XXXXXXX redirect...
使用者 <<<<< 字元串
2、路由系統URL
a. /index/ -> 函數或類
b. /index/(\d+) -> 函數或類
c. /index/(?P<nid>\d+) -> 函數或類
d. /index/(?P<nid>\d+) name='root' -> 函數或類
reverse()
{% url 'root' 1%}
e. /crm/ include('app01.urls') -> 路由分發
f. 預設值
url(r'^index/', views.index, {'name': 'root'}),
def index(request,name):
print(name)
return HttpResponse('OK')
g. 命名空間
/admin/ include('app01.urls',namespace='m1')
/crm/ include('app01.urls',namespace='m1')
app01.urls
/index/ name = 'n1'
reverser('m1:n1')
3、
def func(request):
request.POST
request.GET
request.FILES
request.getlist
request.method
request.path_info
return render,HttpResponse,redirect
4、
render(request, 'index.html')
# for
# if
# 索引. keys values items all
5、
class User(models.Model):
username = models.CharField(max_length=32)
email = models.EmailField()
有驗證功能
Django Admin
無驗證功能:
User.objects.create(username='root',email='asdfasdfasdfasdf')
User.objects.filter(id=1).update(email='666')
class UserType(models.Model):
name = models.CharField(max_length=32)
user_type = models.ForeignKey("UserType")
user_list = User.objects.all()
for obj user_list:
obj.username,obj.email,obj.user_type_id,obj.user_type.name,obj.user_type.id
user = User.objects.get(id=1)
user.
User.objects.all().values("username","user_type__name",)
m = models.ManyToMany('UserGroup')
class UserGroup(models.Model):
name = ....
obj = User.objects.get(id=1)
obj.m.add(2)
obj.m.add(2,3)
obj.m.add(*[1,2,3])
obj.m.remove(...)
obj.m.clear()
obj.m.set([1,2,3,4,5])
# 多個組,UserGroup對象
obj.m.all()
obj.m.filter(name='CTO')
知識點:
URL
- 兩個
Views
- 請求的其他資訊
from django.core.handlers.wsgi import WSGIRequest
request.environ
request.environ['HTTP_USER_AGENT']
- 裝飾器
FBV:
def auth(func):
def inner(reqeust,*args,**kwargs):
v = reqeust.COOKIES.get('username111')
if not v:
return redirect('/login/')
return func(reqeust, *args,**kwargs)
return inner
CBV: 使用者認證 隻用寫這一個。。。。。
from django import views
from django.utils.decorators import method_decorator
@method_decorator(auth,name='dispatch')
class Order(views.View):
# @method_decorator(auth)
# def dispatch(self, request, *args, **kwargs):
# return super(Order,self).dispatch(request, *args, **kwargs)
def get(self,reqeust):
return render(reqeust,'index.html',{'current_user': v})
def post(self,reqeust):
Templates
- 母版...html
extends
include
- 自定義函數
simple_tag
from django import template
from django.utils.safestring import mark_safe
register = template.Library()
@register.simple_tag
def u():
return 123
a. app下建立templatetags目錄
b. 任意xxoo.py檔案
c. 建立template對象 register
d.
@register.simple_tag
def func(a1,a2,a3....)
return "asdfasd"
e. settings中注冊APP
f. 頂部 {% load xxoo %}
g. {% 函數名 arg1 arg2 %}
缺點:
不能作為if條件
優點:
參數任意
filter
@register.filter
def func(a1,a2)
g. {{ 參數1|函數名:"參數二,參數三" }} {{ 參數1|函數名:數字 }}
最多兩個參數,不能加空格
能作為if條件
分頁(自定義的分頁)
XSS:
{{ page_str|safe }}
mark_safe(page_str)
cookie
用戶端浏覽器上的一個檔案
{"user": 'dachengzi'}
session :裝飾器
Models
- 一大波操作
Form驗證
-
緩存
中間件
信号
CSRF
Admin/ModelForm
作業:
主機管理:
1、單表操作
2、一對多
3、多對多
要求:
a. 删除對話框
b. 修改,添加新URL
c. 基于cookie進行使用者認證
d. 定制顯示個數
e. 分頁
預習:
Form: http://www.cnblogs.com/wupeiqi/articles/6144178.html
Model:http://www.cnblogs.com/wupeiqi/articles/6216618.html
登入
res = redirect('/index.html') # 登入
res.set_cookie('user',u) ##設定cookie,關閉浏覽器就失效
return res
v = request.COOKIES.get("user") #首頁
if not v:
return redirect('/login.html')
本文轉自 295631788 51CTO部落格,原文連結:http://blog.51cto.com/hequan/1913161,如需轉載請自行聯系原作者