Monthly Archives: June 2022

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

THE FORM IN DJANGO

THE FORM IN DJANGO In this post we will see how to create a contact form in Django. Let's first see what forms are in Django with slides. I also leave the official link to the documentation Forms. PREPARATION OF THE ENVIRONMENT Let's go over all the fundamental steps for creating a new web application in Django. Virtual environment creation that I called venv with the following command: python -m venv venv Activate the virtual environment with the command: venv\Scripts\activate by positioning yourself in the root folder chosen [...]

By |2024-11-11T18:54:32+00:00June 4, 2022|0 Comments

FORM VALIDATION IN DJANGO

VALIDATION OF THE FORM Django offers us various ways of validating data. In this post we will look at some of the most common and most used techniques. I leave you a link to the official documentation https://docs.djangoproject.com/en/4.0/ref/forms/validation/ A first validation is done on the client side, however for security reasons we cannot rely on this validation only (which can be disabled with the novalidate attribute in the form tag) validation is also required on the server side. A first approach can be to go to the FormContatto class and specify the required attribute. However, [...]

By |2024-11-11T18:55:09+00:00June 4, 2022|0 Comments

THE DJANGO MODEL FORM

Let's start right away, open the visual studio code and create a new app, which we call blog, with the following command: python manage.py startapp blog Once this is done, let's immediately create a simple model in the file models.py. Let's apply the migrations to create the sqlite database with the following commands: python.exe manage.py makemigrations python.exe manage.py migrate I create the forms.py file and edit it like this: As you can see from the image above, this Form inherits from ModelForm. This class does nothing but specify [...]

By |2024-11-11T18:55:47+00:00June 5, 2022|0 Comments

AUTHENTICATION IN DJANGO

First I leave you a link to the official documentation: https://docs.djangoproject.com/en/4.0/topics/auth/default/  the first thing to do is to include the following urlpatterns in our urls.py file: Trying to access accounts we find the list of templates to create shown in the official documentation. I create a templates folder which is at the same level as the apps and configure it in the settings.py file. I'm going to create the registration folder inside. In this folder we will put all the templates we need for authentication in Django. [...]

By |2024-11-11T18:56:32+00:00June 7, 2022|0 Comments

REGISTRATION IN DJANGO

THE USER REGISTRATION FORM CLASS Let's go immediately to create the form. We should use a ModelForm as there is already a user model in Django. The User object. This object contains several fields some of which are not necessary for a registration form. Let's see the code. In this case we have to validate two fields password and confirm_password, and the clean_field_name function is not good. We can use the clean() function for this purpose. I am now going to create the function in views.py which will use the form. [...]

By |2024-11-11T18:57:19+00:00June 7, 2022|0 Comments
Go to Top