vue中實現搜尋過濾功能
阿新 • • 發佈:2020-08-21
檢視
請求相關的屬性方法(request--HttpRequest物件)
def index(request): #http相關請求資訊---封裝--HttpRequest物件 if request.method == 'GET': print(request.body) #獲取post請求提交過來的原始資料 print(request.GET) #獲取GET請求提交的資料 # print(request.META) # 請求頭相關資訊,就是一個大字典 print(request.path) #/index/ 路徑 print(request.path_info) #/index/ 路徑 print(request.get_full_path()) #/index/?username=dazhuang&password=123 return render(request,'index.html') else: print(request.body) # b'username=dazhuang' print(request.POST) #獲取POST請求提交的資料 return HttpResponse('男賓三位,拿好手牌!')
響應相關的方法
HttpResponse --- 回覆字串的時候來使用 render --- 回覆一個html頁面的時候使用 redirect -- 重定向 示例: def login(request): if request.method == 'GET': return render(request,'login.html') else: username = request.POST.get('username') password = request.POST.get('password') if username == 'taibai' and password == 'dsb': # return render(request,'home.html') return redirect('/home/') #重定向 else: return HttpResponse('滾犢子,趕緊去充錢!!!') #首頁 def home(request): return render(request,'home.html')
FBV和CBV
FBV -- function based view
def home(request):
print('home!!!')
return render(request,'home.html')
CBV -- class based view views.py from django.views import View class LoginView(View): # 通過請求方法找到自己寫的檢視類裡面對應的方法 def get(self,request): return render(request,'login2.html') def post(self,request): username = request.POST.get('uname') password = request.POST.get('pwd') print(username,password) return HttpResponse('登入成功!') urls.py url(r'^login2/', views.LoginView.as_view()),
CBV通過不同的請求方法找到對應的試圖類中的方法
關鍵點,反射
def dispatch(self, request, *args, **kwargs):
if request.method.lower() in self.http_method_names:
handler = getattr(self, request.method.lower(), self.http_method_not_allowed) #反射
CBV的dispatch方法
from django.views import View
class LoginView(View):
# GET
def dispatch(self, request, *args, **kwargs):
print('請求來啦')
ret = super().dispatch(request, *args, **kwargs)
print('到點了,走人了')
return ret
def get(self,request):
print('get方法執行了')
return render(request,'login2.html')
def post(self,request):
username = request.POST.get('uname')
password = request.POST.get('pwd')
print(username,password)
return HttpResponse('登入成功!')
FBV加裝飾器
def n1(f):
def n2(*args,**kwargs):
print('請求之前')
ret = f(*args,**kwargs)
print('請求之後')
return ret
return n2
@n1
def home(request):
print('home!!!')
return render(request,'home.html')
CBV加裝飾器
from django.views import View
from django.utils.decorators import method_decorator
def n1(f):
def n2(*args,**kwargs):
print('請求之前')
ret = f(*args,**kwargs)
print('請求之後')
return ret
return n2
# @method_decorator(n1,name='get') #方式三
class LoginView(View):
# GET
# @method_decorator(n1) #方式2 給所有方法加裝飾器
def dispatch(self, request, *args, **kwargs):
# print('請求來啦')
ret = super().dispatch(request, *args, **kwargs)
# print('到點了,走人了')
return ret
# @method_decorator(n1) #方式1
def get(self,request):
print('get方法執行了')
return render(request,'login2.html')
def post(self,request):
username = request.POST.get('uname')
password = request.POST.get('pwd')
print(username,password)
return HttpResponse('登入成功!')