类视图

范式

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中的定义

1 from 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

 

 
posted on 2019-03-21 11:18  華華  阅读(186)  评论(0编辑  收藏  举报