url路由分发之include 装饰器
1 项目文件夹下的urls.py文件中的url写法: 2 from django.conf.urls import url,include 3 from django.contrib import admin 4 from app01 import views 5 urlpatterns = [ 6 # url(r'^admin/', admin.site.urls), 7 #首页 8 url(r'^$', views.base), 9 10 url(r'^app01/', include('app01.urls')), 11 12 url(r'^app02/', include('app02.urls')), 13 14 ] 15 16 app01下urls.py内容写法 17 from django.conf.urls import url 18 from django.contrib import admin 19 from app01 import views 20 urlpatterns = [ 21 # url(r'^admin/', admin.site.urls), 22 url(r'^$', views.app01base), 23 url(r'^index/', views.index), 24 ] 25 26 app02下urls.py内容写法 27 from django.conf.urls import url 28 from django.contrib import admin 29 from app02 import views 30 31 urlpatterns = [ 32 # url(r'^admin/', admin.site.urls), 33 url(r'^$', views.app02base), 34 url(r'^home/', views.home), 35 36 ]
响应相关的方法
1 HttpResponse --- 回复字符串的时候来使用 2 render --- 回复一个html页面的时候使用 3 redirect -- 重定向 4 示例: 5 def login(request): 6 if request.method == 'GET': 7 return render(request,'login.html') 8 else: 9 username = request.POST.get('username') 10 password = request.POST.get('password') 11 if username == 'taibai' and password == 'dsb': 12 # return render(request,'home.html') 13 return redirect('/home/') #重定向 14 else: 15 return HttpResponse('滚犊子,赶紧去充钱!!!') 16 17 #首页 18 def home(request): 19 return render(request,'home.html')
1 def index(request): #http相关请求信息---封装--HttpRequest对象 2 3 if request.method == 'GET': 4 print(request.body) #获取post请求提交过来的原始数据 5 print(request.GET) #获取GET请求提交的数据 6 # print(request.META) # 请求头相关信息,就是一个大字典 7 print(request.path) #/index/ 路径 8 print(request.path_info) #/index/ 路径 9 print(request.get_full_path()) #/index/?username=dazhuang&password=123 10 11 return render(request,'index.html') 12 else: 13 print(request.body) # b'username=dazhuang' 14 print(request.POST) #获取POST请求提交的数据 15 return HttpResponse('男宾三位,拿好手牌!')
1 HttpResponse --- 回复字符串的时候来使用 2 render --- 回复一个html页面的时候使用 3 redirect -- 重定向 4 示例: 5 def login(request): 6 if request.method == 'GET': 7 return render(request,'login.html') 8 else: 9 username = request.POST.get('username') 10 password = request.POST.get('password') 11 if username == 'taibai' and password == 'dsb': 12 # return render(request,'home.html') 13 return redirect('/home/') #重定向 14 else: 15 return HttpResponse('滚犊子,赶紧去充钱!!!') 16 17 #首页 18 def home(request): 19 return render(request,'home.html')
1 FBV -- function based view 2 def home(request): 3 print('home!!!') 4 return render(request,'home.html')
1 CBV -- class based view 2 3 views.py 4 from django.views import View 5 class LoginView(View): 6 # 通过请求方法找到自己写的视图类里面对应的方法 7 def get(self,request): 8 9 return render(request,'login2.html') 10 def post(self,request): 11 username = request.POST.get('uname') 12 password = request.POST.get('pwd') 13 print(username,password) 14 15 return HttpResponse('登录成功!') 16 17 urls.py 18 url(r'^login2/', views.LoginView.as_view()),
CBV通过不同的请求方法找到对应的试图类中的方法
关键点,反射
1 def dispatch(self, request, *args, **kwargs): 2 if request.method.lower() in self.http_method_names: 3 handler = getattr(self, request.method.lower(), self.http_method_not_allowed) #反射
CBV的dispatch方法
1 from django.views import View 2 class LoginView(View): 3 # GET 4 def dispatch(self, request, *args, **kwargs): 5 print('请求来啦') 6 ret = super().dispatch(request, *args, **kwargs) 7 print('到点了,走人了') 8 return ret 9 def get(self,request): 10 print('get方法执行了') 11 return render(request,'login2.html') 12 def post(self,request): 13 username = request.POST.get('uname') 14 password = request.POST.get('pwd') 15 print(username,password) 16 return HttpResponse('登录成功!')
FBV加装饰器
1 def n1(f): 2 def n2(*args,**kwargs): 3 print('请求之前') 4 ret = f(*args,**kwargs) 5 print('请求之后') 6 return ret 7 return n2 8 @n1 9 def home(request): 10 print('home!!!') 11 return render(request,'home.html')
CBV加装饰器
1 from django.views import View 2 from django.utils.decorators import method_decorator 3 4 def n1(f): 5 def n2(*args,**kwargs): 6 print('请求之前') 7 ret = f(*args,**kwargs) 8 print('请求之后') 9 return ret 10 return n2 11 12 # @method_decorator(n1,name='get') #方式三 13 class LoginView(View): 14 # GET 15 # @method_decorator(n1) #方式2 给所有方法加装饰器 16 def dispatch(self, request, *args, **kwargs): 17 # print('请求来啦') 18 ret = super().dispatch(request, *args, **kwargs) 19 # print('到点了,走人了') 20 return ret 21 22 # @method_decorator(n1) #方式1 23 def get(self,request): 24 print('get方法执行了') 25 return render(request,'login2.html') 26 def post(self,request): 27 username = request.POST.get('uname') 28 password = request.POST.get('pwd') 29 print(username,password) 30 return HttpResponse('登录成功!')
1 def bge(f): 2 def inner(*args,**kwargs): 3 前戏 4 ret = f(*args,**kwargs) 5 收工 6 return ret 7 return inner 8 9 @bge 10 def index(request): 11 return render(request,'xx.html') 12 13 from django.utils.decorators import method_decorator 14 15 @method_decorator(bge,name='get') 16 class Index(View): 17 @method_decorator(bge) 18 def dispatch(self,request,*args,**kwargs): 19 请求前干点儿事 20 ret = super().dispatch(request,*args,**kwargs) 21 请求后干点儿事儿 22 return ret 23 24 @method_decorator(bge) 25 def get(self,request): 26 ... 27 def post(self,request): 28 ...
本文来自博客园,作者:竹石2020,转载请注明原文链接:https://www.cnblogs.com/ch2020/p/13054358.html