CBV进阶(一)

时间:2022-05-03 23:55:26

http_method_names , http_method_not_allowd

用途:views只接受这些请求.

范围View

Default:['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace']

get_context_data(self, **kwargs)

用途:传递额外的参数

范围:TemplateView

示例

class HomePageView(TemplateView):

    template_name = "home.html"

    def get_context_data(self, **kwargs):
context = super(HomePageView, self).get_context_data(**kwargs)
context['latest_articles'] = Article.objects.all()[:5]
return context

  

get()

额外用途:传递额外的object_list到templates中

范围ListView

allow_empty:

用途:如果为True展示空列表,如果不为空报404错误.

model = Foo相当于queryset=Foo.objects.all()

get_quersyset()默认返回queryset