Django For Beginners
A comprehensive introduction to Django for beginners
This tutorial aims to help beginners getting started with Django, a popular Web Framework written in Python. To get maximum from this content, the audience should be familiar with a terminal and have a minimal set of tools already installed. Python3, a modern code editor (VsCode, Atom), and GIT versioning command-line tool should be enough to experiment with all the code.
Install Python
The core of dependency for Django is Python and we should install the interpreter first. Most of the systems come with Python already installed and we can easily check in the terminal:
If the version displayed in the terminal is Python2, please note that this version has been discontinued for versioning and development since Jan.2020. To download and install Python access the official page, select the installer that matches the operating system, and follow the installation setup. Once the process is finished, recheck the Python version in the terminal.
Manage Dependencies
With Python up and running, we can install Django and other modules required by our development. The recommended way to install and manage the dependencies for a Python project is to use a virtual environment, a safe way to isolate the dependencies across multiple projects.
For Windows-based systems, the syntax is slightly different:
Let's install Django, using PIP (official package manager for Python)
The above command will install the latest stable version of Django. From this point, we can use all tools provided by Django to create a new project, apps and manage the project via Django CLI.
Install a Code Editor
This section has plenty of options from the old-school (yet modern) Vim to VsCode and Atom. For those unfamiliar with any of these tools, VsCode might be a good choice to get started fast.
Build a Django Project
A new project can be generated with ease in Django by using django-admin that provides a collection of settings for the database, Django, and security layer.
Create the project folder
Inside the directory, we will generate the core of our project using django-admin tool :
Note: Take into account that .
at the end of the command.
Create the database and the app tables
Start the application
At this point we should see the default Django page in the browser:
Create New Application
In the previous section, we've generated the core of the project that handles the configuration and now we will create the first Django application to serve a simple page to the users.
Add a new route - edit
app/views.py
The next step is to inform Django that we've created a new app and update the routing to include the new definition.
Update the configuration to include the new app -
core/settings.py
Update the routing rules as below -
core/urls.py
After saving all changes we should no longer see the Django default page (the one with the green rocket):
Create New Model
Even simple applications require minimal storage for persistent data. Django provides out-of-the-box an SQLite database, just to help us start fast. During this section, we will create and interact with a new table (model).
Visualize the default SQL settings -
config/settings.py
The ENGINE
specify the technology used to manage the database. In our case is a lightweight (yet powerful) SQLite engine. NAME
informs Django where to save the database on the filesystem.
Define a new model
Books
insample
application. The below changes should be added tosample/models.py
:
Tip - for a quick check over the latest changes we can run check
subcommand.
Generate the SQL code (
migrate
the database).
Apply changes on the database
Use the model via CLI
Once the model is created we can use it via the Django shell:
List all items (books) (using the CLI)
We can see our new book returned by the query. Let's improve the information that describes the object.
Django Model - add text representation of an object
To achieve this goal, we should define the __str__()
method for the Book
model
Let's restart the Django console and check the results:
Using Admin Section
Django comes by default with a usable admin dashboard that allows us to manage all app models and users with ease. In order to access the module, a superuser
should be created using the Django CLI:
Create Django Superuser
CRUD (create, read, update, delete) actions are provided for all default models (users, groups) shipped by Django. To register the new models, Books
in our case, we need just a few lines of code.
Register
Book
model to be visible in theadmin
section
After this small change in our code, we should see Books
model listed in the admin page:
Django Admin - Edit Book Items
Thanks for reading! For more topics, feel free to contact Appseed.
Resources
Read more about Django (official docs)
Start fast a new project using development-ready Django Starters
Last updated