DJANGO

DJANGO ARCHITECTURE

DJANGO INTRODUCTION Before starting to talk about the Django Framework I leave you a link to the official documentation which will come back very useful later. https://docs.djangoproject.com/it/4.0/. The main purpose of this first dedicated section is to provide a good overview of the Framework, and of the main parts that make it up. This will facilitate the understanding of the role played by the individual components, which we will analyze and use in detail in the following sections. WHAT IS DJANGO Django is a high-level Web Framework written in Python, which enables the development of secure and scalable websites [...]

By |2022-10-26T00:41:07+00:00May 18, 2022|0 Comments

FIRST PROJECT IN DJANGO

FIRST PROJECT IN DJANGO VIRTUAL ENVIRONMENTS The first thing we have to do to work on virtual environments is to create a folder, once the created folder has been selected, with the cd command in Windows, or ls in Linux we give the following command: python -m venv virtual_name_environment This command creates the virtual environment with all the files necessary for the project to work, including Python binaries. The following image shows the virtual environment just created in the visual studio code. Once the environment has been created, it must be activated [...]

By |2024-11-11T18:47:34+00:00May 20, 2022|0 Comments

FIRST APPLICATION IN DJANGO

FIRST DJANGO APPLICATION After having created the first project with the command django-admin startproject project_name it is time to analyze the individual files created. WE CREATE OUR FIRST APPLICATION Let's go to visual studio code and create our first application with the above command, positioning ourselves in the directory where the manage.py file is contained. Let's analyze the files that make up a Django application. __init__.py an empty file [...]

By |2024-11-11T18:48:22+00:00May 22, 2022|0 Comments

MODELS AND MIGRATIONS

MODELS AND MIGRATIONS We will see in this post after the creation of a news application, how it is possible to define through classes and OOP entities (classes) that they represent the conceptual model of our App. We will do this by writing the models.py file of our application. Once the conceptual model has been written and its relationships established, we will create the Sqlite database using the migration mechanism. Let's start by creating a new virtual environment, after creating the root folder that I called secondlevel. We issue the following commands to create [...]

By |2024-11-11T18:49:26+00:00May 24, 2022|0 Comments

THE VIEWS IN DJANGO

DATABASE API Before dealing with the views, let's see how to interact further with the database, add data, modify them, delete them, make filters etc. Django provides APIs that allow us to interact with the database at a higher level than SQL (Structure Query Language). LET'S BEGIN! We open our project and in the terminal we type the following command: python manage.py shell This command is used to open a shell where we will type commands. First of all, let's import the Giornalista entity with the following command. from news.models import [...]

By |2024-11-11T18:50:17+00:00May 25, 2022|0 Comments

THE TEMPLATES IN DJANGO

THE TEMPLATES Templates are nothing more than html files set in the views in the render method. The render method accepts three parameters, the request, the html file we want to render and a third parameter, the context which is a data dictionary in which we are going to specify what we want to display in the html page. We will be able to set up the html file so that it displays articoli and giornalisti. We decide where the html file resides, we go to the settings.py file [...]

By |2024-11-11T18:51:01+00:00May 29, 2022|0 Comments

THE URLS IN DJANGO

THE URLS IN DJANGO URLS are used to map the resources of our application, and are fundamental in the Django architecture. We create an urls.py file within the news application and include it in the project's urls.py file. As shown in the image. While the application's urls.py file we modify it as follows: We have seen that the path function is used to map a specific url to a view. In the image above you can see how angle brackets are used to define the detail of [...]

By |2024-11-11T18:51:46+00:00May 29, 2022|0 Comments

STATIC FILES IN DJANGO

STATIC FILES IN DJANGO Static files are images, css, javascript files, fonts etc. in essence we turn to all those components that then represent the front-end part. Django in ready-to-use applications provides us with the django.contrib.staticfiles application that we need static files. Let's go to the settings.py file of the project and scroll the page until we find the STATIC_URL entry. Let's specify the directory that will contain our static files, such as Bootstrap. Now let's create the static folder in BASE_DIR remembering that this constant indicates the folders immediately [...]

By |2024-11-11T18:52:27+00:00May 30, 2022|0 Comments

INHERITANCE BETWEEN TEMPLATES

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 [...]

By |2024-11-11T18:53:10+00:00June 1, 2022|0 Comments

CLASS BASED VIEWS

DETAIL VIEW The DetailView allows us to get the details of a single object, while with the Generic Display Views ListView we get the details of a list of objects. They are very comfortable because in addition to being particularly performing they are also easy to implement. Let's go to the views.py file and implement a DetailView. After specifying the class that inherits from DetailView, you need to specify the model and template name. As for the latter, we will use the existing articoloDetail.html. We then go to manage the class [...]

By |2024-11-11T18:53:54+00:00June 2, 2022|0 Comments
Go to Top