Thursday, October 16, 2025

Blog Project Creation (Chap 1-3) Django 5 By Example Book – Credit: Antonio Mele 5th Edition.

Share

I started reading this book to understand Django framework a lot better and I wanted to post these items on this site as a digital notebook to come back to for my reference or anyone who wants to use. I will continue to do these types of posts with other books and add or tutorials I see or read. I will start this post from where are left and ended the ‘Introduction to Python Django.’

First, we start with the MODEL, this is the source where all your data lives. Its one database table, depending on how many you create for your application. Each field within the MODEL is actually a field within the database. Here are some links below if need for more information and explanation. I will be using POSTGRESQL

Github link to Django 5 by Example Book.

PostgreSQL & Django Model Explained Below:

PostgreSQL link

Django MODEL Explained.

This is what we type into the MODEL file first to start the first project within this book. If you need to filed information about this code use the link above.

The following lines of code are self explanatory for each post that is created within the application. Here are the last three lines addred to the code above. These items do the tracking of post for the web application, they do what their names says at the beginning of the line.

Learning about Index Field in django 5 Click Here- Index.

Read up on the USERS Model as well – Click Here: User Model

Finished Output of Model Below:

Databases & Relationships Explained.

These are the items learned in this section working on the Model below for this Blog application:

Many to One Relationship Link, Click Here.

Working with Migrations in Django, Click Here.

Databases Explained by Django Framework.

Creation of Admin Site for Application – Super User…

Type the following in terminal of your application – python manage.py createsuperuser

Next, add username and then your password you want to use.

Next, go to the site admin page – http://127.0.0.1:8000/admin/

Your output should look like this below:

Once inside you see this below:

Those two models you see there within the picture (Groups and Users) is part of the Django Authentication framework. The Users Model is where you can use it for login and other authentication for your application, I will explain further into each chapter of this book.

Adding a Model To the Admin.py File:

That is the simple way to add a Model to the admin.py file. Now below this line you see it done again by the author by using decorators and all the bells and whistles to make the administration backend layout look customized for easy viewing and display. The first line of code which starts with the @ symbol is the decorator. The is the customization of the admin backend.

There is a last line of code we add under ‘ordering’ it is this ==> show_facets = admin.ShowFacets.ALWAYS ==>< now this just tells you the reading or coding that each time you add a post it counts it for you. I added three posts and it showed within the FILTER side bar under (draft= 3). You go through the tutorial as I am doing and it shows up that way. Click Here – FACETS new to 5.0

Django Databases Explained- Working with QuerySets (ORM – object relational mapper):

This is an important topic and I would read up on this a lot more for understanding and clarity. I will mention some key points and provide links for you to go to and do your own research so you can understand how it works. One thing I would mention is, if you work with MySQL in the past there are lots of similarities and Django ORM just uses a different name of what they do in the same way MySQL does it.

Django ORM is an API that allows for full CRUB (create, update, retrieve, and delete). Django ORM also allows for doing SQL queries as I mentioned to talk to the db. This is the working mapping system that takes your MODEL you created and talks to your database with the Python language. That simple and with ease. Very Cool stuff here. Click Here

Where will you connect the db: you go into the settings.py file and look for ‘Databases’ and add all your configuration there to connect to any database of your choice. QuerySet API Model Documentation Click Here. also Making queries in Django ORM

I STRONGLY RECOMMEND YOU READ UP ON THIS AREA TWICE AND GET A GOOD UNDERSTANDING THE DATABASE AND HOW DJANGO USES IT.

Adding The Postgresql db Engine To A Django Project

First download the PostGreSQL from the following link. Click Here

Next, check version on your command line after you download and install. Type this ==> (postgres –version or postgres -V) either one will work.

Next, MAKE SURE YOUR POSTGRESQL DB IS RUNNING BEFORE YOU TRY TO LOOK IT UP WITHIN THE COMMAND LINE TO RUN SOME COMMANDS. AFTER YOU HAVE THAT UP AND RUNNING (START SERVER). TYPE THIS IN CMD LINE –> psql

Next, after your type [psql] you get your prompt, type [\l] (letter L), and it will list all databases within your server.

The following short snips will help using the command line with postgresql.

  • \d (list the databases)
  • \q (quits the psql)
  • \du (list the roles of each user in system)
  • create user >ADD USERNAME YOUR WANT HERE< with password ‘ADD PASSWORD HERE IN QUOTES’ superuser;
  • grant all privileges on database >NAME OF DATABASE HERE YOUR CREATED OR USING< to >USERNAME YOU ARE<;

How to create postgres user.

How to change password in postgres video

How to create a postgres user Fazila Malik 2024

SETTINGS.PY File in project area find this and change the information.

I used the following to run the server postgresql and the access tables to view data I used pgAdmin and the command line tool. I am sure there is some other way making it easier than what I did. I will find that and change up the process in the future.

Hopefully, this clears up a little on how to connect the db within the Django application. Following that I will touch on the final pieces of the Django puzzle to make the full connection from database, Views.py (logic of application), urls.py (routing of application), and templates (html pages of application / for user viewing on front end of application).

Views.py page of Chapter 1 book example, exact code credit to author in title.

The important piece within this code I would pull for me is the {‘posts’: posts} on the return statement as this is where your use the (posts) within your for loop to pull from the database. I was lost looking to see how it pulls from the db and I kept changing the name of db to something else that never connected. So, remember use that name or (context) when we change as your name for the data.

Context Feature within “return statement.”

The context feature within the return statement is used for rendering a template (HTML file). It’s a dictionary that give you the ‘key:value pair. It literally becomes a reference where you can access and use as a variable.

Django Context Explained link

Get Object or 404 Error Shortcut **

This is a shortcut in the Django framework used to pull an object from the database. It raises a Http404 error is the object is not found within the database. A link below is provided for further explanation if needed.

get_object_or_404() use this instead of try except. However, read up on it as its used within the views.py

URLs.py file understanding linked below:

These are the links to your django application, basically the menu you provide and how the routing is done within your site. Read up on it before starting your application to get an understanding of such.

urls.py django 5 documentation.

Templates Pages and HTML for Viewing:

These are the pages that render your data from the database and view on the client side of the application.

templates pages django 5 documentation and understanding.

{% load static %} – Make sure to read up on this to load your static files to application.

This pertains to all your – Javascript files, CSS files, images. etc. Link Static Files Click Here.

Request / Response Cycle Understanding Image. Follow this and Read over several times to understand well. Credits – Author – Antonio Mele

Django Framework is a Full Stack Application (Backend as well) or can Connected to React Native (Front End) – Learn Enough To Become Dangerous and Create Your Idea.

Read more

Local News