python的Web框架,類視圖
阿新 • • 發佈:2019-03-21
section teacher itl 指示 分發 XML 定義 -m 文件中
類視圖
範式
1 from django.views import View 2 3 # 繼承View 4 class IndexView(View): 5 def get(self, request): 6 #寫法和函數視圖一樣 7 pass
在urls中調用的時候,和函數視圖配置是不一樣的。
# as_view() 類視圖是需要運行 path(‘index/‘,views.IndexView.as_view(), name=‘index‘)
通用視圖
簡單應用
view中的定義
1from django.views.generic import ListView 2 3 class StudentListView(ListView): 4 # 指定使用的頁面 5 template_name = ‘teacher/student_list.html‘ 6 7 # 獲取模型數據 8 model = Students 9
html 中應用
# 此處的students_list,為view中定義的name,如果不定義則為object_list {% for student in students_list %}<p>{{ student }}</p> {% endfor %}
模塊使用
1 class StudentListView(ListView): 2 # 指定使用的頁面 3 template_name = ‘teacher/student_list.html‘ 4 5 # 獲取模型數據 6 model = Students 7 8 # 對html中的獲取的名字進行修改 9 context_object_name = ‘students_list‘10 11 # 每頁顯示的數據 12 paginate_by = 3 13 14 # 頁面名稱 15 section = ‘學生列表‘ 16 17 # 搜索字段 18 # search = self.request.GET.get(‘search‘,‘‘).strip() 19 20 21 # 通過這個方法,改變傳遞在前面的模板對象列表。 22 def get_queryset(self): 23 search = self.request.GET.get(‘search‘,‘‘).strip() 24 per_page = self.request.GET.get(‘per_page‘, 5) 25 self.paginate_by = int(per_page) 26 27 if search: 28 if search.isdigit(): 29 sts = self.model.objects.filter(Q(qq=search)|Q(phone=search),is_deleted=False).order_by(‘-e_time‘) 30 else: 31 sts = self.model.objects.filter(name_contains=search,is_deleted=False).order_by(‘-e_time‘) 32 else: 33 ses = self.models.objects.filter(is_delete=False).order_by(‘-e_time‘) 34 return sts 35 36 37 38 # 上下文管理,context內容傳給html 39 def get_context_data(self, **kwargs): 40 context = super().get_context(**kwargs) # 繼承父類 41 context[‘section‘] = self.section 42 context[‘search‘] = self.request.GET.get(‘search‘,‘‘).strip() # 獲取的搜索內容 43 context[‘page‘] = int(self.request.GET.get(‘page‘, 1)) # 獲取的當前頁碼 44 context[‘per_page‘] = self.paginate_by 45 46 #繼承的ListView自動幫我們做了分頁,會根據self.paginate_by,page,per_page的參數來進行分頁,和之前寫的當前頁的數據(sts=paginator.get_page(page))是一個意思。 47 context[‘students‘] = context[‘page_obj‘] 48 return context
pege_obj html中的使用
{{ page_obj }}
在網頁端顯示的方式是一個對象:
<Page 1 of 6>
DetailView
from django.views.generic import DetailView class StudentDetailView(DetailView): template_name = ‘teacher/detail‘ model = Students
# urls.py中 應用 path(‘detail/<int:pk>‘,view.StudentDetailView.as_view(),name=‘detail‘)
html 中應用
{{ object.name }}
{{ object.sex }}
{{ object.studentsdetail.college }}
類視圖的權限裝飾
在urls.py文件中使用
from django.contrib.auth.decorators import login_required path(‘student/‘,login_required(views.StudentListView.as_view()),name=‘stuent‘) # 一般使用在項目根urls中
view中使用的權限類裝飾
1 方法一:定義函數 2 3 from django.utils.decorators import method_decorator 4 5 class StudentListView(ListView): 6 pass 7 當前的通用類視圖沒有顯式指示一個get、post 8 9 # 分發get,post 10 @method_decorator(login_required) 11 def dispatch(self, request, *args, **kwargs): 12 return super().dispatch(*args, **kwargs) 13 14 def get_queryset(self): 15 16 def get_context_data(self.**kwargs):
1 方法二:裝飾類 2 3 @method)decorator(login_required, name=‘dispatch‘) 4 class StudentListView(ListView): 5 pass
python的Web框架,類視圖