Is Django Easy to Learn for a Beginner
It can take a long time before beginners are absolutely comfortable working with Django. It's important to have a good understanding of Python before diving into Django. In this post, I will cover what I believe to be the most important things to understand in Python before learning Django.
If you are comfortable with Python and you're considering going into web development, you have a few options to choose from. Django is the most popular Python web framework. But Django is also not the simplest technology to learn. It can take a long time before beginners are absolutely comfortable working with Django.
However, it's important to have a good understanding of Python before diving into Django. Learning Django will undoubtedly be more difficult. In hindsight, I realize that some of the times I was struggling with Django were actually not Django issues at all - they were simply misunderstandings of Python.
In this post, I will cover what I believe to be the most important things to understand in Python before learning Django. I will also explain where these concepts are used in Django, and why it makes sense to understand them.
If you prefer video format, you can watch the video version of this tutorial here:
Disclaimer - this post will not be teaching the actual concepts. Use it rather as a guide for what to learn. I will include links for learning resources.
Basics
- Variables
- Data types
- Conditional statements
- For-loops
These are the building blocks of not only Python, but all programming languages. It's crucial to understand these concepts before going further. Variables, data types, conditionals, and for-loops are used constantly in programming. These are not unique to understanding Django.
Iterables
An iterable is an object that can be "iterated over", such as in a for-loop.
In Python, the most common way of looping through data is by using a list
. Lists are simply constructed like this:
names = ['joe', 'soap']
Another common way of loop through data is to use a tuple
like so:
plants = ('flower', 'tree')
Django has its own objects to store data.
In Django, you will work with a Queryset
. Querysets are objects that you could describe as very powerful lists of data. Not only can you loop through the queryset, but you can perform complex operations on them too - such as filtering and comparing.
It's very important to understand that a Python list is described as an iterable. A tuple is also an iterable. And a Django queryset is also an iterable
The takeaway from this is to understand what it means to be an iterable and to "iterate over" an object, such as in a for-loop. This concept of iterating is something you will use constantly when working with Django.
Dictionaries
Dictionaries are commonly used in Python to store key-value pairs of data. For example:
person = { "first_name" : "Joe" , "last_name" : "Soap" , "age" : 25 }
When working with Django, one of the most common places you'll work with dictionaries is when adding context
to a template.
Context is a dictionary of information you'd like to have access to inside of an HTML template. Context is one of the most fundamental concepts of working with templates in Django, and so it's crucial to understand it first as a Python object.
It is also good to understand methods on a dictionary such as .update()
and .pop()
.
Functions
Functions are one of the most important concepts in programming. Not only in Python, but in all programming languages.
The way that functions are created in Python is quite simple:
def print_name (name) : print ( f"Hello {name} !"
You will be writing plenty of functions when working with Django. Hence it's very important to understand them before diving into Django.
One of the concepts that is left out in most courses that teach Python functions is the concept of *args and **kwargs
This guide from RealPython is a must-read for those who are not familiar with it. In Django, you will be using *args and **kwargs
a lot, so understanding this concept now will be a serious help.
Decorators
In my experience, decorators are not used that often. But they can be very useful in solving certain problems in your code. Django makes use of decorators to provide some helpful utilities, hence it makes sense to understand them.
The syntax of using decorators is fairly simple, but what happens inside them is what you should focus on learning:
def uppercase_decorator (function) : def wrapper ( ) : func = function( ) make_uppercase = func.upper( ) return make_uppercase return wrapper @uppercase_decorator def print_name ( ) : return "joe soap" # JOE SOAP
This guide from DataCamp will help you understand decorators.
Classes
This is where the real learning begins.
Classes are crucial to understand before working with Django
This is where I fell short when I started learning Django. I believed my understanding of everything else was good enough but didn't focus on learning classes.
But most of the way you write code in Django is by using classes.
You write Models
, Forms
, and Views
using classes. In fairness, you can write Views
using functions, but I believe you'll be missing out on a much simpler way to write them.
Even if you decide to write Views
using functions, most of how Django works internally is through classes.
Classes are so important that I'm breaking up this section into the subsections of what you need to know about Classes before starting with Django.
Fundamental Concepts
The fundamentals are:
- Syntax of writing a class
- The Object-Oriented Programming paradigm
- The
self
keyword - The special
__init__
method - The difference between an instance and a class
- Inheritance
- Methods (instance methods and class methods)
All of this can be learned by following this post by RealPython.
Overriding methods
When inheriting from a class, you can override existing logic on that class. This is something you will do countlessly when working with classes provided by Django. For example:
class Person : def __init__ (self, name, age) : self.name = name self.age = age def full_name ( ) : return f" {self.name} " class Employee (Person) : def __init__ (self, name, age, title) : self.name = name self.age = age self.title = title def full_name ( ) : return f" {self.title} {self.name} " # e.g Mr Soap
Here we are creating an Employee
class that inherits from the Person
class. We are overriding the full_name
method to return something different.
In Django, you will override methods when working with Models
, Forms
and Views
, so understanding what overriding does, is an important concept in OOP.
The super() function
The super()
function is very important to understand when working with classes. I've specifically put it as its own subsection to highlight it. This is because when working with Django you will use the super()
function a lot.
To understand super()
you need to understand inheritance first. So make sure you've already gone through the previous list of concepts about classes.
Here is a great post by RealPython that explains the super()
function.
As an example, when working with context in Django Views
, you will call the super()
function to grab the already existing context, so that you can customize it:
class ExampleView (TemplateView) : def get_context_data (self, **kwargs) : context = super (ExampleView, self) .get_context_data( **kwargs) context.update( { "name" : "Joe Soap" } ) return context
This snippet brings together a lot of the concepts in this post:
- The
ExampleView
is inheriting from theTemplateView
- The
get_context_data
method takes in**kwargs
- We call the
super()
function to get the already existing context from Django - The context is a dictionary, so we call the
.update()
method to update the dictionary with our own data - We return the context at the end of the method, as Django requires it
Packages
When working with Django you will typically import a lot of functions and classes from Django's modules. If you are not used to importing and working with libraries and third-party packages, you might find it to be an extra layer of learning for you.
For this reason I recommend starting out with other Python libraries like pandas
and numpy
, so that you can get used to the idea of importing and working with packages.
This also brings up another important concept to understand, which is the difference between a library and a framework. You can read an in-depth breakdown of this on FreeCodeCamp, but this is the idea:
When you use a library, you are in charge of the flow of the application. You are choosing when and where to call the library. When you use a framework, the framework is in charge of the flow. It provides some places for you to plug in your code, but it calls the code you plugged in as needed.
This is very important to understand because Django is a framework, not a library. If you're not familiar with working inside a framework, it might require a change of mindset to understand how everything fits together.
Conclusion
Django is not the easiest tool to learn. It requires a strong foundational knowledge of Python and particularly good familiarity with classes and Object-Oriented Programming.
I personally recommend spending a good amount of time becoming very familiar with Python, building lots of projects - even if they don't have anything to do with web development. And once you feel strong about your Python skills, then diving into Django.
Source: https://justdjango.com/blog/how-much-python-before-django
0 Response to "Is Django Easy to Learn for a Beginner"
Post a Comment