INHERITANCE BETWEEN TEMPLATES
The main idea behind the concept of inheritance between templates is to write an HTML file that we will call base.html in which we will write much of the code. The common code in other HTML pages will end up in the parent template.
Let’s create the base.html file in the templates folder of the project, then go to the Bootstrap site, copy the starter template and paste it into the newly created HTML page. Now suppose we want to extend homepage.html and articoloDetail.html so we want to specify the code common to these two pages in the parent base.html template. Let’s now define some code blocks in base.html within which the code of the pages we are extending will be placed.
The two tags highlighted in red create a sort of window in which the code of the child templates to be inserted will then be specified. Let’s start editing the article page, at the top we write this tag:
{% extends ‘base.html‘ %}
We delete everything and leave only the content.
When the articoloDetailView function will render the html page then Django will notice the extends tag and will put the content between the block content tag and the end of the tag in the same tags in the parent base.html template. Let’s now extend the homepage.html as shown in the figure with the same procedure.
Let’s start the development server and check if everything is working.
THE DJANGO ADMIN PANEL
The administration panel is a feature for administrators only, if we were to make a site open to the public, this section must remain private. Let’s go into the project and create a superuser with the above command. As you will have noticed in the file urls.py the admin section is already configured, there is a url to access it. As mentioned, this panel allows CRUD functionality for our models. Let’s see how to configure the admin.py file.
Now let’s enter the admin panel by adding the /admin part to the home page url.
To remove the plural, go to the models.py file and modify it as follows:
It is a good idea every time you work on the model to migrate with the commands:
python.exe manage.py makemigrations
python.exe manage.py migrate
Leave A Comment