Beginner learning - Function base or Class Base approach

English isn’t my first language, so sorry about the grammar, and weird way organize sentence. I end up here is because after researching the community for Django I find out the English community were way more helpful.

Goal for learning Django : Planning to learn the Django fundamental and fully understand the idea of how it’s work, not just using it by following other’s tutorial making stuff. I want to reach the level that I can only using documents and my brain to create something I like.

Background:

  • 6 months in my self-taught journey, knowing all basic fundamental concepts and syntax of Python, HTML, CSS, Javascript. Mainly trying to focusing on the backend. For Django I had follow their tutorial, and recently I’m read the book “Django for Beginners(5th Edition)”

Problem :

  • I can see the benefit of Class-base approach more fit into DRY principle.

  • BUT ! I had a feeling that I’m not fully get the idea of class, class inheritance or the idea of OOP. I think I understand the concepts of class , but when come to using it. It’s always had the unsure what I’m doing.

  • So, for beginning of the Django learning phase should I start with making basic project by using the “function-base” approach, until I could easily making whatever I’m trying to do, than start move on to “class-base” approach ? What are you guys do when start learning Django ?


Side Question

  • Python journey of how you get to your current level ?
    I see Python as a language that can script mostly anything faster base on it’s easy to read syntax, and this is my goal and reason why I start my coding journey, not because I want to get a job. I want to have ability to use it on daily basis, such as scraping data I’m interesting, create some tool I want to use … etc.
    So, I assume the person going to answer were the people that already get to this level , could you guys share some your Python journey of how you get to your current level ?

  • How to learn/read or use the documents ?
    I’m not saying looking up guide video were bad, some of it were very helpful, but sometime it’s just very hard to find quality guide or the specific things I’m looking for. So,
    how you guys using documents? if possible please try to recall the memories that when you just starting learning to code, and what/how you reach the level you currently at.

  • Except doing project, what else you do for getting better in your coding journey?
    I fully get the idea of making project is best way to learn, but sometimes I feel my ability were not enough. So, "How you guys approach something outside of your understanding to push you become better?

For anyone who spend time finish reading or response it, I appreciate your time. Thank you.

CBVs vs FBVs is still a hotly debated concept. I think you have to trust your own judgment ultimately. I personally think CBVs don’t contribute enough value to make them worth using.

Congratulations on making it this far in your journey, and welcome to the Django and Python communities.

There are strong arguments for both function-based views (FBVs) and class-based views (CBVs). I won’t try to settle that debate, but I will say you can absolutely write successful software with either approach.

If FBVs are making more sense to you right, go for it! There’s a pretty well-known, opinionated guide walking through FBVs: Django Views — The Right Way. Maybe it will give you some confidence in pursuing that style for some initial projects.

As you mention, doing projects is an important part about getting better at writing code. Other practices that should accelerate your learning:

  • Read the code you’re calling. The amazing thing about open source is it’s all there for you to read. I personally enjoy using an IDE (PyCharm) that makes it really easy to navigate a code base. Clone a copy of Django’s source code from Github and poke around.
  • Practice debugging skills. We’re always pursuing a better mental model of what is going on in our computers. Using a debugger you can step through the execution of your Django app, line by line, and see the state of everything.
  • Write tests or experiments for anything you’re not sure about. Build confidence that you can prove things to yourself and others about how things work.
  • Find a way to collaborate with programmers more experienced than you.

I can only second this.

Personally, I’ve found that the more complex my view and form logic gets, the more do I appreciate the simple and straightforward approach of FBVs, whereas CBVs only introduce additional layers of abstraction and complexity.

In my opinion, CBVs are suitable for writing basic CRUD views with minimal code, but both for simpler and for more complex views, enjoy the clear structure of FBVs.

For crud views you’d probably want a higher level library than CBVs anyway imo. Like iommi (of which I am a co-author), or neapolitan, or something like that.

That’s soo great!
Helpful

Welcome @ligmartinis !

Class-based views and Function-based views

In our case, it’s not “either / or”, it’s both.

Keep in mind that CBVs are Python classes. The Django-provided Generic CBVs are only one possible implementation of them. (For example, in addition to the alternatives listed in the earlier responses, there is also django-vanilla-views.)

Those Generic CBVs can also be considered a starting point and not an ending point. They can be extended in a large number of ways.

For example, we have a rather intricate security layer for determining whether the current user can access a particular view. We have implemented this as a mixin.

I also have a set of mixins that cover a couple of common cases that aren’t directly handled by the Django Generic CBVs - one that is designed to handle two models joined by a foreign key (such as “User” and “Profile” combination), and one designed to handle “model and formset”.

We then created a set of classes that include those mixins in their definition, and use those classes in the views as the base classes for our CBVs.

From our perspective, it’s not about “DRY”, it’s about ensuring consistency among the views. (Personally, I think “DRY” tends to be overemphasized.)
All the views being used extend our custom base classes, ensuring that they all implement the same requirements the same way.

On the other hand, not everything needs to be a CBV, and I don’t hesitate to write an FBV if the situation warrants it.

Python / Django journey

I worked with Python for more than 5 years before working with any Python-based web framework, and 10 years before seriously working with Django. (I think the first version of Python I used was something like 1.5 or 1.6, when it was a much simpler language.)

As I was the only one using Python in my job, my resources were books and on-line resources such as the Python mailing lists. I had to read a lot to wrap my head around certain concepts back then - usually with a lot of trial and error and error and error.

In my case, it was by forcing myself to do it. Even if I had bought a book on a particular topic, and felt like I learned what I needed to learn from the book, I would always make it a practice to go back and read the documentation about that same topic.

For me, that accomplishes three objectives:

  • Gain familiarity and comfort with how the docs are written and organized. If I think I’ve understood what a book is telling me, seeing how the docs present the same information helps familiarize me with using the docs themselves.
  • Gives me a second perspective on the same topic. This tends to help me understand more about a topic, perhaps expanding my comprehension beyond just the direct question I’m encountering.
  • Gives me the exposure to other things that either the book doesn’t cover or covers elsewhere. Just scrolling through the docs to find a particular reference will also mean that my eyes scan topic areas that I should be aware of.

(Side note: I have no patience for video tutorials for most programming topics. I find that they’re generally not worth my time.)

I read good code. I look for popular / highly-rated projects and dig in to see if I can understand how they work.

I encourage people to dig into Django itself and read how it implements certain features. (I don’t suggest digging into the ORM as a starting point. It’s not that it’s not good code, but because of the complexity - those modules are some of the most sophisticated Python code that I have ever tried to read. I’d say right now I understand maybe 20% of it.)

Reading the implementation of the CBVs can be quite helpful, and resources such as the Classy class-based views site and the Class-Based Views diagrams can be very helpful with understanding them.

1 Like

ligmartinis

  • 6 months in my self-taught journey, knowing all basic fundamental concepts and syntax of Python, HTML, CSS, Javascript. Mainly trying to focusing on the backend. For Django I had follow their tutorial, and recently I’m read the book “Django for Beginners(5th Edition)”

First of all knowing your background what i will suggest is not think django for backend only even if you know HTML, CSS & Javascript. Use django for full development process and use your existing knowledge to get desired output in django template. Because you are starting learning django so you have to first realise that everything you wish can be fulfilled by django itself. Once you learned the thing you can think to use separate frontend technologies to see as solutions for the limitations you are facing. But before that you must have limitations and reasons to choose.

  • I can see the benefit of Class-base approach more fit into DRY principle.

As You are learning the things so don’t conclude anything at this stage. But to learn the things perfectly. That how you can implement CBV and How FBV. Next you have also to try to use both ways for the same output. Once you will be able to use both only then you can understand when to use which.

  • BUT ! I had a feeling that I’m not fully get the idea of class, class inheritance or the idea of OOP. I think I understand the concepts of class , but when come to using it. It’s always had the unsure what I’m doing.

In that case instead of writing codes and using concepts. Start reading codes. You will find number of project source codes on GitHub.

Python journey of how you get to your current level ?

Programming is not only knowing the syntax and writing the codes. You code to instruct the solution of a programming problem to the computer or Network. See programming as a skill which can think as how codes get executed, how computer works & How network response. See things as you have to use Computer & network as a resources and get things done using it infinite number of times. & For that you write codes.

How to learn/read or use the documents ?

djando documentation is amazing place to start. But you might be getting trapped by clicking hyperlinks in that. Jumping somewhere eles where you was not intended to be. What i will suggest that download the PDF of documentation from the documentation page. This will let you go in a flow.

Except doing project, what else you do for getting better in your coding journey?

Not jupm into what others are doing which have years of experience. For the time being what I will advise you is Thinking & Solving limitations of packages. You might be using many third party packages. Just think that there was limitation of some kind so someone developed that solution. Realising challenges soliving them. Come the the forum read the topics (Problems & Solutions) go to Stackoverflow and read question & Answers.

1 Like