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.
Thank you for reading! TL;DR: Links and Resources
- Django – a few reasons to use it
- Scaffolding a Django Project and start it
- Create a Django template
- Create a new model and use it
- Accommodate with Django Shell
- How to access the admin section
- Django Volt Bootstrap 5 – design crafted by Themesberg
- Django Soft UI Design System – provided by AppSeed
- Django Datta Able – a simple and colorful dashboard
Django – Reasons to use it
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:
- Django Extensions – provides many useful helpers
- Django-environ – manage the environment variables with ease
- Django REST framework – a flexible toolkit for coding APIs fast
- Django-Allauth – a popular module for social logins using Google, Facebook, Github, and many more
To learn more than this article provides, feel free to access the official Django website and documentation.
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.
1# – Check Python version
$ python --version Python 3.8.4
2# – Create and activate a virtual environment
# Unix based systems $ virtualenv env $ source env/bin/activate
3# – Start the project
$ $ python manage.py runserver ... Django version 3.1.7, using settings 'firstdjango.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CTRL-BREAK.
If all goes well, by visiting the browser we should a nice “Welcome” page generated for us by Django.
Django – Starter Homepage
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 manage.py 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”
Create a Django Template
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
Django – 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 “models.py” 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)
2# – Generate the SQL code and effectively add the table to the database:
$ python manage.py makemigrations test_app $ python manage.py migrate
This powerful feature allows us to interact with project code, inspect classes, call helpers and query the database.
$ python manage.py 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 manage.py shell >>> from django.contrib.auth.models import User >>> all_users = User.objects.all()
In the same manner, we can update the information, a user for instance.
$ python manage.py shell >>> from django.contrib.auth.models import User >>> user = User.objects.get(username="testuser") >>> user.is_admin = True >>> user.save()
Access the admin section
Django unlocks the “administration” section only for superusers. To create a “superuser” we should use the Django shell:
$ python manage.py createsuperuser
Once the “superuser” is created we can authenticate and manage visually all tables and users defined in the project.
Django Administration Dashboard
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. All mentioned projects can be downloaded from Github and the permissive license allows the usage for hobby and commercial projects.
Django Soft Design System
A simple Django starter crafted on top of Soft UI Design System, a modern Bootstrap 5 UI Kit. Soft UI Design System is built with over 70 frontend individual elements, like buttons, inputs, navbars, navtabs, cards or alerts, giving you the freedom of choosing and combining. Django Codebase is already enhanced with a database, session-based authentication, helpers, and deployment scripts.
- Django Soft Design System – product page
- Django Soft Design System – LIVE Deployment
Django Soft UI System – Open-Source Seed Project
Django Bootstrap 5 Volt
Open-source Django Template seed project crafted with authentication, basic modules, deployment scripts on top of a modern jQuery-free design – Volt Dashboard. This Dashboard Template is designed on top of Bootstrap, the world’s most popular framework for building responsive, mobile-first sites. Volt Dashboard brings 11 example pages including an overview, sign in, sign up, transactions page and many more. There are 3 lightweight and Vanilla JS plugins that come with Volt, namely a date picker, notification and charts library.
- Django Bootstrap 5 Volt – product page
- Django Bootstrap 5 Volt – Demo – LIVE deployment
Django Volt Dashboard – Free Django Starter.
Datta Able Django
Production-ready Django dashboard coded with authentication, database, modular codebase on top of a modern UI: Datta Able admin dashboard (free version). Datta Able rendering is fast in all major browsers. It is passed through a good score in Google Page Speed, Pingdom, GT Metrix. Code passed via w3 validators with all w3 standards. This admin panel is fully responsive and tested on all retina devices.
- Datta Able Django – Product page
- Datta Able Django – Demo – LIVE Deployment
Django Datta Able – Free Django Starter.