Curso Django. Plantillas II  Variables y propiedades en plantillas. Vídeo 6

Curso Django. Plantillas II Variables y propiedades en plantillas. Vídeo 6

Introduction to Django Templates

In this section, we will learn about using variables in Django templates and how to access objects and their properties. We will also explore the use of dictionaries within the context.

Using Variables in Templates

  • Variables can be referenced in templates by using the context dictionary.
  • To utilize a variable value in a template, enclose it within double curly braces and provide the corresponding key from the dictionary.
  • Multiple values can be retrieved by adding more key-value pairs to the context dictionary.

Example Implementation

  • An example is shown where two variables, "nombre" (name) and "apellido" (surname), are added to the context dictionary.
  • The values of these variables are then accessed in the template using their respective keys.

Conclusion

Using variables in Django templates allows us to dynamically display data from our Python code. By passing a context dictionary to the template rendering process, we can easily access and display variable values within our HTML templates.

Using Variables in Python

In this section, the speaker discusses how to use variables in Python and mentions that variables can be used to store values and access them later. The speaker also explains that variables can be utilized directly in the context without explicitly mentioning the variable name.

Storing Values in Variables

  • Variables are used to store values.
  • Sometimes it is necessary to mention the variable where the value is stored.
  • Other times, values can be used directly in the context without referencing the variable.

Accessing Variables in Templates

  • To access information from a variable in a template, add a new key-value pair to the dictionary.
  • Use a unique key name (e.g., "moment_actual") and assign it with the variable storing the current date and time.
  • In the template, access this information using double curly braces and the key name (e.g., moment_actual ).

Formatting Dates and Times

  • Dates and times can be accessed as properties of objects using dot notation.
  • To format dates or times differently, modify how they are accessed from the object property.
  • For example, accessing only the day would be done by using "moment_actual.day", while accessing both day and month would require "moment_actual.day/month".

Creating Custom Classes

  • It is possible to create custom classes in Python.
  • These classes can have constructors that accept parameters for initializing instance variables.
  • Within views or templates, instances of these classes can be created by passing appropriate arguments to their constructors.

Programming Oriented Towards Objects

This section focuses on programming oriented towards objects. The speaker explains how properties of objects can be accessed using dot notation. They also mention that viewers should refer back to previous videos on object-oriented programming if they need further clarification.

Accessing Object Properties

  • Properties of objects can be accessed using dot notation.
  • In templates, use the key name from the dictionary followed by a dot and the property name to access specific properties of an object.

Custom Classes

  • It is possible to create custom classes in Python.
  • These classes can have constructors that accept parameters for initializing instance variables.
  • Instances of these classes can be created within views or templates by passing appropriate arguments to their constructors.

Further Learning

  • If viewers are having trouble understanding any concepts related to object-oriented programming, they are encouraged to refer back to previous videos on Python and review the content provided there.

New Section

This section explains the usage of variables and dictionaries in Python, specifically in the context of object-oriented programming.

Accessing Properties of Objects

  • In object-oriented programming, properties of objects can be accessed using dot notation.
  • For example, to access the name property of a person object, we can use p.name.
  • Similarly, to access the surname property, we can use p.surname.

Link to timestamp

New Section

This section demonstrates how to update and display information from a dictionary in a template.

Updating Information in a Template

  • When updating information in a template, we need to use the same key names as before.
  • After making changes in the view file, save them and refresh the browser (using F5).
  • The updated information should now be displayed correctly in the template.

Link to timestamp

New Section

This section emphasizes that accessing properties and objects will be further explored later on. It highlights that understanding dictionaries is essential for this purpose.

Accessing Properties and Objects

  • Accessing properties and objects is straightforward once you understand dictionaries in Python.
  • More complex concepts related to accessing properties and objects will be covered later on.

Link to timestamp

Playlists: Curso Django
Video description

Seguimos trabajando con las plantillas. Vemos en este vídeo cómo usar variables en plantillas y cómo acceder a objetos python desde las plantillas. Para más cursos, ejercicios y manuales visita: www.pildorasinformaticas.es