Curso Django. Plantillas I. Vídeo 5

Curso Django. Plantillas I. Vídeo 5

Introduction and Video Frequency

In this section, the instructor introduces the course and addresses concerns about video frequency.

  • The instructor greets the viewers and mentions that some videos took longer to upload due to being on vacation.
  • Assures viewers that video frequency will increase in the future.

Introduction to Templates

This section introduces templates, explaining what they are and their purpose in Django.

  • Templates are text strings that can contain HTML code.
  • They separate the logical part of a project from its visual part.
  • Previously, HTML code was embedded within Python views, which is considered bad practice.
  • Templates allow for easier modification of design and logic separately.
  • They facilitate collaboration between designers and programmers working on a project.

Using Templates

This section explains how to use templates in Django.

  • The most common way is to save the HTML code as a separate file in a folder.
  • The template file is then loaded into the view using a template object.
  • Advantages of using templates include separating HTML code from Python code, allowing for independent work on design and logic, and enabling dynamic content creation through contexts.

Creating Templates

This section outlines the three basic steps involved in creating templates.

  1. Create a template object by calling the Template class with the path to the external HTML file.
  1. Create a context containing any additional data needed for dynamic content (even if it's empty).
  1. Render the template object using the render method, passing it the context as a parameter.

Implementing Templates in Project

This section demonstrates how to implement templates in a Django project.

  • Open the terminal and start the server.
  • Open the views file and locate the desired view.
  • Replace the embedded HTML code with a template object, loading the external HTML file.
  • Create a context, even if it's empty.
  • Render the template object using the render method, passing it the context as a parameter.

The remaining part of the transcript is not included in this summary.

Creating the First Template

In this section, the speaker discusses the process of creating a template in Django by separating the code from the template. The speaker demonstrates how to create a new HTML file and save it in a designated folder within the project.

Creating a New HTML File

  • Open Visual Studio Code and create a new file.
  • Copy and paste the HTML code into the new file.
  • Modify the text content as desired.

Saving the HTML File

  • Create a new folder called "templates" within the project directory.
  • Save the HTML file inside this "templates" folder.

Loading and Rendering the Template

  • Go to the views.py file.
  • Remove the embedded HTML code and replace it with loading and rendering instructions for an external template.
  • Create a variable to store the loaded template using open() method to specify its path.
  • Create an object of type Template using template.Template() constructor, passing in the loaded document as an argument.
  • Close communication with the loaded document using close() method of open().
  • Create an empty context variable using context.Context() constructor.

Loading External Template Manually

In this section, we learn how to load an external template manually by specifying its path. The speaker explains how to adjust backslashes in Windows paths and complete the path to locate our desired template.

Specifying Path for External Template

  • Use double backslashes or forward slashes when specifying paths on Windows systems.
  • Complete the path by adding additional folders if necessary.

Organizing Templates in Separate Folder

This section emphasizes organizing templates by creating a separate folder for them. The speaker explains that while it is not mandatory, it is good practice to keep templates organized within their own dedicated folder.

Creating a Templates Folder

  • Create a new folder within the project directory and name it "templates" (or any desired name).
  • Save all template files inside this "templates" folder.

Naming and Saving the Template File

In this section, we learn how to name and save the template file. The speaker demonstrates how to save the HTML file with a specific name in the previously created templates folder.

Naming the Template File

  • Choose a suitable name for the template file.
  • Save the HTML file with the chosen name.

Loading and Rendering External Template

This section focuses on loading and rendering an external template in Django. The speaker explains how to load an external document using open() method, create a Template object, and close communication with the loaded document.

Loading External Template

  • Specify the path of the external template using open() method.
  • Adjust backslashes in Windows paths if necessary.
  • Complete the path by adding additional folders if needed.

Creating Template Object

  • Create a variable to store the loaded template using template.Template() constructor, passing in the loaded document as an argument.

Closing Communication with Loaded Document

  • Use close() method of open() to close communication with the loaded document.

Following Python Naming Conventions

In this section, we discuss following Python naming conventions when creating variables. The speaker acknowledges that while they may not always adhere strictly to these conventions due to teaching multiple programming languages, it is important to be reminded of their significance.

Python Naming Conventions

  • Functions should have names separated by underscores for readability.
  • Variable names should follow conventions for better code organization and maintainability.

Creating Context for Template

This section covers creating a context for the template. The speaker explains that since the current template is simple and does not require dynamic content, an empty context will be used.

Creating an Empty Context

  • Create a variable to store the context using context.Context() constructor.
  • Leave the context empty as there is no dynamic information to be included in this particular template.

Loading External Template Manually

In this section, we learn how to load an external template manually by specifying its path. The speaker explains how to adjust backslashes in Windows paths and complete the path to locate our desired template.

Specifying Path for External Template

  • Use double backslashes or forward slashes when specifying paths on Windows systems.
  • Complete the path by adding additional folders if necessary.

Loading External Template Manually

In this section, we learn how to load an external template manually by specifying its path. The speaker explains how to adjust backslashes in Windows paths and complete the path to locate our desired template.

Specifying Path for External Template

  • Use double backslashes or forward slashes when specifying paths on Windows systems.
  • Complete the path by adding additional folders if necessary.

Adjusting Backslashes and Completing Path

This section focuses on adjusting backslashes in Windows paths and completing the path to locate the desired external template. The speaker demonstrates these steps using Visual Studio Code.

Adjusting Backslashes

  • Change the direction of backslashes in Windows paths from left-leaning ( to right-leaning (/).

Completing Path

  • Add any additional folders required within the project directory.

Creating Template Object

In this section, we create a Template object by loading an external document into memory. The speaker demonstrates how to use the template.Template() constructor and assigns it to a variable.

Creating Template Object

  • Create a variable to store the loaded template using template.Template() constructor.
  • Pass the loaded document as an argument to the constructor.

Closing Communication with Loaded Document

This section covers closing communication with the loaded document after creating a Template object. The speaker explains that this step is necessary to avoid consuming unnecessary resources.

Closing Communication

  • Use close() method of open() to close communication with the loaded document.
  • This ensures that resources are not unnecessarily consumed.

Creating Empty Context

In this section, we create an empty context for the template. The speaker explains that since this is our first template and it does not require dynamic content, no information needs to be included in the context.

Creating Empty Context

  • Create a variable for the context using context.Context() constructor.
  • Leave the context empty as there is no dynamic information required for this template.

Rendering the Document

In this section, the speaker discusses how to render a document using a variable called "documento" and the "render" method.

Creating and Rendering the Document

  • To render the document, a variable called "documento" is created to store the rendered output.
  • The rendering process is done using the "pallet" variable, which holds the open document.
  • The "render" method is called on "pallet", passing in the context as an argument.

Viewing Rendered Page

This section explains how to view the rendered page in a web browser after making changes to the template.

Checking Changes in Browser

  • After saving changes made to the views file, we can visit the same URL in a web browser.
  • Instead of displaying "Hola alumnos," it should now show our specified message from the HTML template.
  • By refreshing (F5) the page, we can see that our changes have taken effect.

Separation of Design and Code

Here, it is discussed how separating design (HTML) and code (Python) allows for independent work by programmers and designers.

Advantages of Separation

  • With separation of design and code, one person can work on Python code while another works on HTML design.
  • Designers can modify HTML styles independently from Python code.
  • Demonstrates changing style by adding CSS properties like color to an element.

Simplifying Multiple Views

This section highlights potential issues when loading multiple views and introduces loaders as a solution for optimization.

Issues with Multiple Views

  • Loading multiple views requires calling methods like open and close for each view, which is not optimized.
  • Incorrect file paths or missing files can cause errors.

Using Loaders

The speaker introduces loaders as a more elegant solution to handle multiple views and address the issues mentioned earlier.

Solving Issues with Loaders

  • Loaders help optimize the loading process of views.
  • They provide a more elegant way to handle incorrect file paths or missing files.
  • Demonstrates intentionally providing an incorrect file path to showcase the error message.

Dynamic Content in Templates

This section mentions upcoming videos that will cover introducing dynamic content into templates using examples from previous videos.

Introducing Dynamic Content

  • Explains how to add dynamic content like dates and calculations to templates using variables passed from Python code.
  • Upcoming videos will cover this topic in more detail.

The summary has been provided in English as requested.

Playlists: Curso Django
Video description

Comenzamos a ver en este vídeo las plantillas. Las plantillas en Django nos permiten separar la parte lógica de la parte visual en los documentos html. Para más cursos, ejercicios y manuales visita: www.pildorasinformaticas.es