3 reasons why you should learn function-based views first

knivets

Dima Knivets

Posted on April 21, 2020

3 reasons why you should learn function-based views first

You're fairly new to Django, and there're a lot of unfamiliar concepts you need to wrap your head around. A common source of confusion for beginners is different kinds of views: function-based views, class-based views, generic views. You might be confused about which type of views would be best for you to start with or which views you should use in the future.

First of all, you don't need to learn about all types of views right now, you just need to start with one. You shouldn't worry about which type is the best one either since, at this point in your journey, you need to pick the option that would help you to learn as much as possible in the easiest way. The best type of views to learn for a beginner is function-based views (FBVs). There are three reasons why you should start with FBVs.

A function is a much easier abstraction than a class

There are essentially two kinds of views in Django: function-based and class-based. The difference between them boils down to the difference between functional and object-oriented (OOP) paradigms. In general, OOP is confusing to beginners because it is a more complex abstraction.

On the other hand, you learn about functions early on, and since there're fewer concepts to grasp, they are easier to learn. FBVs are a pretty thin and straightforward abstraction: they receive a request as an argument and return a response, that's it. The views are mapped directly to URL routes, so there is no magic in between.

FBVs are easier to read and work with

With CBVs, it is not always apparent what functionality is there, which methods are being called, and in what order. Sometimes, to understand the logic flow of a CBV, you have to follow the inheritance hierarchy closely to see what gets called and when.

Unlike CBVs, functions are explicit: you can learn about a function's behavior simply by looking at its body. There is no hidden functionality or magic. It is clear what arguments are being passed or what functions are being called. Following the logic flow of a function is a much easier task than with CBVs. As a result, they are much easier to understand and debug.

Using FBVs is a better learning experience

CBVs are a more advanced and complex tool. At the same time, they solve problems that are not relevant to you if you're a beginner. A popular flavor of CBVs is generic views, which allow you to quickly prototype common use cases without writing a lot of code. It might be tempting to use these views from the beginning, however, you won't be able to learn much. If they break, you would have no idea what to do, since you don't know how they work.

That's why FBVs are a better option when it comes to learning: since you can't use generic views, you have no choice but to implement the functionality from scratch. Building something from scratch is a great learning experience because it helps you to really understand how something works. Finally, once you feel comfortable building common functionality, it will be much easier to learn and understand CBVs or generic views.

What should I do next?

Generic CBVs implement a functionality which is often called CRUD (create, read, update, delete). CRUD operations are performed on an object, which is an instance of a Django model. So why not go ahead and build a similar functionality from scratch using FBVs? For example, start with a view that performs a read operation, which involves fetching an object and rendering a template with that object. You could later move on to the rest of the CRUD operations.

πŸ’– πŸ’ͺ πŸ™… 🚩
knivets
Dima Knivets

Posted on April 21, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related