我一直在学习Django,我有一个混淆的来源是基于类的视图以及何时重写get方法。我查看了文档,它解释了什么,但它不能解释什么时候应该重写get。何时重写Django CBV中的get方法?
我最初创建一个视图是这样的:
class ExampleView(generic.ListView):
template_name = 'ppm/ppm.html'
paginate_by = 5
def get(self, request):
profiles_set = EmployeeProfile.objects.all()
context = {
'profiles_set': profiles_set,
'title': 'Employee Profiles'
}
return render(request, self.template_name, context)
但我最近告诉我的代码很简单的足够的默认实现,所有我需要的是这样的:
class ExampleView(generic.ListView):
model = EmployeeProfile
template_name = 'ppm/ppm.html'
所以我的问题是这样的:在什么情况/情况下我应该重写get方法?
注意这个例子还是不需要你覆盖'得到()' - 这将是更好的覆盖['get_queryset()'](https://docs.djangoproject.com/en/ 1.9/ref/class-based-views/mixins-multiple-object /#django.views.generic.list.MultipleObjectMixin.get_queryset)。 – Alasdair