Django Cheat Sheet - And Free Samples

This article is just another Django Cheat Sheet that might help beginners to speed up their learning curve and code much faster projects with commercial value. For newcomers, Django is the most popular Python-based web framework initially released in 2003. Since then, Django has become a reference framework in the web development ecosystem mostly for the “batteries-included” concept and built-in security patterns coded by experienced developers. To make this article more useful and trigger curious minds into programming, a few open-source Django projects will be mentioned.

Thanks for reading! Topics covered by this article

Section #1 - Django Presentation

At the moment this article is published, Django is actively supported by 2k+ contributors with monthly releases and security fixes that keep up the Django core with the latest patterns and concepts used in production. Big tech companies like Instagram and Disqus use Django as the main technology for their core services and this argument might be enough to convince beginners to take a look at this amazing web framework. Being in production for more than 15 years,  Django community members released in the open-source ecosystem many useful libraries. I will mention just a few below:

To learn more than this article provides, feel free to access the official Django website and documentation.

Section #2 - Scaffolding a Django Project

The goal of this section is to generate a minimal Django starter using the console. before we start, it might be a good idea to check if Python is properly installed and accessible in the terminal.

Step 1# – Check Python version

$ python --version
Python 3.8.4
Django Cheat Sheet - Check Django Version

Step #2 - Create and activate a virtual environment

# Unix based systems
$ virtualenv env
$ source env/bin/activate
Django Cheat Sheet - Create VENV

Step #3 - Create the project

$ mkdir hellodjango 
$ cd hellodjango
Django Cheat Sheet - Create Project Folder

Once our working directory is hellodjango (feel free to use another name), the next step is to call django-admin and generate the project:

$ django-admin startproject config .
Django Cheat Sheet - Generate Project

Step #4 - Start the project

$ python runserver 
Django Cheat Sheet - Start Project

If all goes well, our newly created Django app should be visible in the browser.

Django Cheat Sheet - Project Default Screen.

Section #3 - Create a Django App

Django by default has a modular codebase based on apps. This pattern impacts the project maintenance and evolutions in a positive way because features are isolated and test-able. When a legacy project needs to be enhanced with a new core feature we might use a new app for this and the necessary steps are listed below:

  • Navigate to the root project folder
  • Create the app running the command $python startapp <new_app_name>
  • Inform Django that a new “app” is registered (this is not done automatically) by adding “app name” to INSTALLED_APPS section in the core settings file
  • Execute Django migration and we are good to go with the new app
$ python migrate
$ python runserver
Django Cheat Sheet - Migrate the new App

Section #4 - Create a new Template Page

Using templates in our projects is something that helps us to win time and reuse components when new pages are built. Django comes with an intuitive structure regarding this topic as shown below:

Django – Template Files

Once the template is defined, we can serve the file to users:

Django – Serve Template File

Section #5 - Create a new Model

In Django world, a model stands for a new table required by our project to save new information. Please take a look at the necessary steps to define and use a new model:

1# – Update the file saved in the app directory with the new table definition

from django.db import models

class Employee(models.Model):
	first_name = models.CharField(max_length=30)
	last_name = models.CharField(max_length=30)
        role = models.CharField(max_length=10)
Django Cheat Sheet - Create Model

2# – Generate the SQL code and effectively add the table to the database:

$ python makemigrations test_app
$ python migrate
Django Cheat Sheet - Migrate Database

At this point, we can use the new model in our project.

Section #6 - Django Shell

This powerful feature allows us to interact with project code, inspect classes, call helpers and query the database.

$ python shell
Django Cheat Sheet - Invoke Django Shell

The above command typed in the root of any Django project will open the interactive shell and we do many things with ease. To see this in action, let’s select all registered users.

$ python shell
>>> from django.contrib.auth.models import User
>>> all_users =  User.objects.all()
Django Cheat Sheet - List Users in Shell

In the same manner, we can update the information, a user for instance.

$ python shell
>>> from django.contrib.auth.models import User
>>> user = User.objects.get(username="testuser")
>>> user.is_admin = True
Django Cheat Sheet - Create User in Shell

Section #7 - Access ADMIN Section

Django unlocks the administration section only for superusers. To create a superuser we should use the Django shell:

$ python createsuperuser
Django Cheat Sheet - Create Superuser

Once the superuser is created we can authenticate and manage visually all tables and users defined in the project.

Django Administration Dashboard - Django Cheat Sheet.

With all this fresh information in mind, it might be a good idea to start coding something useful on top of a few production-ready starters provided by Creative-Tim and AppSeed App Generator. All mentioned projects can be downloaded from Github and the permissive license allows the usage for hobby and commercial projects.

Argon Dashboard Django

This simple starter comes with a few useful features like authentication, database and deployment scripts out-of-the-box. The product is built on top of Argon Dashboard design (free version) and might be used to bootstrap faster a new project by any developer with basic programming knowledge.

Django Cheat Sheet Sample - Argon Dashboard.

Soft UI Dashboard Django

Admin Dashboard generated by AppSeed in Django Framework. Designed for those who like bold elements and beautiful websites, Soft UI Dashboard is ready to help you create stunning websites and web apps. Soft UI Dashboard is built with over 70 frontend individual elements, like buttons, inputs, navbars, nav tabs, cards, or alerts, giving you the freedom of choosing and combining.

Django Cheat Sheet Sample - Soft UI Dashboard.

Material Dashboard Django

Start your development with a beautiful Material Admin template for Django. Material Dashboard makes use of light, surface, and movement. The general layout resembles sheets of paper following multiple different layers so that the depth and order is obvious. The navigation stays mainly on the left sidebar and the content is on the right inside the main panel.

Django codebase is crafted using a simple, modular structure that follows the best practices and provides authentication, database configuration, and deployment scripts for Docker, a popular virtualization software.

Django Cheat Sheet Sample - Material Dashboard.

Django Volt Bootstrap 5

Admin Dashboard generated by AppSeed in Django Framework. Volt Dashboard is a free and open-source Bootstrap 5 Admin Dashboard featuring over 100 components, 11 example pages, and 3 plugins with Vanilla JS. There is more than 100 free Bootstrap 5 components included some of them being buttons, alerts, modals, date pickers, and so on.

Django Cheat Sheet Sample - Volt Dashboard.

Black Dashboard Django

Creative Tim partnered with AppSeed App Generator to provide better products for developers and designers. Our beautiful UI Kits are integrated into production-ready starters enhanced with database, authentication, modular codebase, and deployment scripts for well-known configurations like Docker, Gunicorn/Nginx.

Django codebase is crafted using a simple, modular structure that follows the best practices and provides authentication, database configuration, and deployment scripts for Docker, a popular virtualization software. Any developer with basic Django/Python knowledge, by following the product documentation should be able to compile and use the app by typing only a few lines in the terminal.

Django Cheat Sheet Sample - Black Dashboard.

Thank you for reading! For more resources, please access: