Introduction to Django Concepts
When it comes to learning the Django framework, there can be a lot to learn. What I want to do here is highlight some of the key concepts that every beginner should aim to learn and master. Knowing and being able to apply these concepts will ensure you are capable of building out most of the functionality you see on websites today and can make you a potential candidate for a job.
As always, with learning these concepts comes practice and implementation. But before we start, I want to quickly mention my complete Django course that's linked in the video description. In this course, you will learn about all the concepts that I mentioned here and more while applying what you learned to build out a platform for developers to display their profile and share their work. Okay, let's start with the first concept that every Django developer should know.
Structuring Projects with Django Apps
At the heart of every Django website are apps. Apps are small pieces of our project that make up an entire website. We create apps to represent different parts of a website, and how you structure your apps is completely up to you. Apps typically contain some kind of database models, URLs, views, and other information about a particular part of our website.
Think about this structure for a minute and imagine we were building facebook.com with Django. You can have one app that represents all the information and logic about your users. Another app that contains all your database tables, logic for posts, and things related like likes and comments for those posts—all inside of one app. When it comes to Facebook groups, that would be best to put that into its own app so all that logic is separate from other parts of our website.
All these apps would sit inside of a main project folder, and you would simply let Django know about these apps by registering them inside of your application settings file.
Handling Requests with Views
Views are simply functions that are in charge of processing a user's request when they visit a certain URL or endpoint on our website. When a user goes to our website, some view associated with that URL is responsible for some logic in the back end and returning some form of response with data, and this is typically some kind of HTML template or JSON data.
Views can also be represented as classes, and these are called class-based views. The major difference between function-based views and class-based views is how they extend logic. For anybody just starting to learn Django, I would highly recommend you first start by learning function-based views because they are easier to understand and most of the documentation is written using function-based views. However, I think it's necessary to have a good understanding of class-based views and how to effectively use them.
URL Routing and Patterns
To handle URL routing in a Django application, we create URL patterns in a list and simply attach different paths to those views. This is how Django knows which view to fire off when a user visits a URL on our website. In this example, we would simply use the path function, set a URL route, and assign an associated view.
Django's Templating Language
Nowadays, with the popularity of building APIs and front-end frameworks like React, Angular, and Vue, you may end up not even needing traditional templates in your application. With that being said, there's still a good chance you may use them, so a solid understanding of the Django templating language is still important.
Models and the ORM
Models are simply class-based representations of our database tables and are at the core of how we design our database. With models, we simply create a class that represents a table, and the attributes in this class represent each column inside of that database table. Knowing how to create relationships between these tables, such as one-to-many and many-to-many, is also a must.
Along with knowing how to design your database, you also need to know how to work with this database. Simple methods like get, all, and filter are how we retrieve items from the database using Django's built-in ORM.
The Built-in Admin Panel
Django provides us with a quick start interface to work with our data. While personally I like to build out my own interface in any application I make, Django also gives us this admin panel to view, create, and update any data in our database. The admin panel is a great tool to start with and is also highly customizable.
While building most any application, you probably will have to perform CRUD operations at some point. This stands for Create, Read, Update, and Delete. While you do have the admin panel that allows you to do a lot of this, you should know how to build all of this functionality out on your own. We can perform tasks like this by writing all of our own code from scratch, using methods like the save and delete method, or we could use Django model forms and or class-based views that handle a lot of this functionality for us.
In either case, you should know how to perform all of these tasks in different ways.
Static files are essential to every website as they add in a visual layer to our application, like images, CSS, and JavaScript, along with any other static assets. Knowing how to configure your static assets along with being able to handle user-uploaded content such as profile pictures or any other content that the user may upload is one of the core essentials of many applications.
User Authentication
Lucky for us, Django makes authentication very simple with all of its built-in methods and forms. With this, every beginner should be capable of handling user registration, login, logout, and password reset functionality. With password reset, there is however an added layer because you will need to understand how to configure emails in order to send out an email when a user requests a new password.
Essential Management Commands
Django has a whole list of built-in commands for us to use, so I'll keep it simple by narrowing it down to five commands that you'll probably use right away in any application. The startproject command creates the boilerplate Django files. The startapp command gives us the boilerplate for an app and all the files associated. makemigrations preps our database for migrations, and migrate executes these migrations and actually updates the database. The createsuperuser command allows us to create a user in our database with admin-level permissions.
Using Django Signals
Signals are extremely powerful for when we need to listen for events that occur inside of our application. You'll need to learn how to create senders and receivers to listen for events and fire off actions whenever these events occur. One way I like to use signals is to add in event listeners for anytime a new user registers on my website. When this happens, I fire off an action that simply sends out a welcome email to that user with any extra information they might need.
Production Databases
While Django does provide us with an SQLite database to start building our project, most applications you'll work with will need some form of production-level ready database like MySQL or PostgreSQL. You'll need to know how to make this connection inside of your settings file and also how to work with that specific database.
Building APIs with Django REST Framework
As I mentioned before when I was talking about templates, many developers will skip building templates in Django altogether in their application and just end up building out some kind of API with Django on the back end and use a front-end framework like React or Angular on the front end. This is a viable option, and I've actually done this in multiple applications myself. With that being said, learning how to use the Django REST Framework with Django in this process has really become a standard. You may be able to build out your applications without it, but there's a solid chance at some point you'll need it, and an even better chance that any job you apply to will expect you to know how to work with and use the Django REST Framework.
Deploying Your Application
Okay, so on this part, I'm going to go a little bit easier on beginners because it does include other technologies, but I do think every Django beginner should be capable of deploying their application. This means being familiar with platforms such as Heroku, AWS, or any other platform that allows you to deploy your application and host your database and static assets.
Conclusion
It's essential to not only know how to build your application on a local machine but also how to deploy it and make sure it's production ready. Alright, so those are my 15 concepts that I think every Django developer should master. Don't forget to subscribe if you enjoyed this video and make sure to check out the course linked in the video description.