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
- Argon Dashboard Django – simple Django starter with Argon Design
- Django Volt Bootstrap 5 – design crafted by Themesberg
- Django Soft UI Design System – provided by AppSeed
- Material Dashboard Django – a popular design now available in Django
- 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
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 https://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.
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:
Once the template is defined, we can serve the file to users:
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.
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.
Argon Dashboard is a popular Bootstrap 4 template provided with a rich UI and pre-built pages like Dashboard, user profile, Google maps, and authentication pages. Django codebase is crafted using a simple, modular structure that follows the best practices and provides authentication, database configuration, and deployment scripts for Docker and Gunicorn/Nginx stack. All components can take variations in color, that you can easily modify using SASS files. You will save a lot of time going from prototyping to full-functional code, because all elements are implemented.
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.
This product came as a result of users asking for a material dashboard after we released our successful Material Kit. We developed it based on your feedback and it is a powerful bootstrap 4 admin dashboard, which allows you to build products like admin panels, content managements systems and CRMs. Material Dashboard comes with 5 color filter choices for both the sidebar and the card headers (blue, green, orange, red and purple) and an option to have a background image on the sidebar. The Django codebase being a simple one, any developer with a 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.
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.
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.
More Django Resources
Thank you for reading! For more Django resources, feel free to access:
- A curated index with more Django Templates provided by Creative-Tim
- More Django Dashboards provided by AppSeed
- More Django Templates – a curated list published on Dev.to