What is the advantage of Class-Based views?

后端 未结 5 379
-上瘾入骨i
-上瘾入骨i 2020-12-12 11:19

I read today that Django 1.3 alpha is shipping, and the most touted new feature is the introduction of class-based views.
I\'ve read the relevant documentation, but I fi

5条回答
  •  Happy的楠姐
    2020-12-12 11:46

    One way to think about class based views, is that they are like a the Django admin with training wheels off and therefore a lot more flexible (but more difficult to understand).

    For example the list-display in the admin is clearly based on the generic ListView. The simplest list view you would only define a model or queryset.

    class MyExampleView(ListView);
        model = ExampleModel 
    

    You will need to supply your own template, but it will basically be the same as the most basic ModelAdmin. The list_display attribute in the model admin will tell it what fields to display, whereas in the ListView you would do this in the template.

    class SpeciesAdmin(admin.ModelAdmin):
        list_display = ['name']
    admin.site.register(ExampleModel , ExampleModelAdmin)
    

    With the admin you have a parameter

    list_per_page = 100
    

    which defines how many objects per page. List view has

    paginate_by = 100
    

    which achieves the same thing. Likewise if you look into customising the admin heavily, you will see a lot of overlap.

    This site here should give you a better idea of what they do as well.

    http://ccbv.co.uk/

提交回复
热议问题