What is the advantage of Class-Based views?

后端 未结 5 385
-上瘾入骨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条回答
  •  孤城傲影
    2020-12-12 11:46

    You can subclass a class and refine methods like get_context_data for specific cases, and leave the rest as-is. You can't do that with functions.

    For instance, you might need to create a new view that does everything a previous one does, but you need to include extra variable in the context. Subclass the original view and override the get_context_data method.

    Also, separating the steps needed to render the template into separate methods promotes clearer code - the less done in a method, the easier it is to understand. With regular view functions, it's all dumped into the one processing unit.

提交回复
热议问题