Variables
Let's learn about the method and syntax of variables.
We'll cover the following
Variables
As we recall from the previous lesson, a template contains variables, which get replaced with values when the template is evaluated.
Syntax of a variable
A variable looks like this: {{ variable }}
.
Variable names consist of any combination of alphanumeric characters and the underscore (_
), but they may not start with an underscore.
NOTE: We cannot have spaces or punctuation characters in variable names.
Use a dot (.)
to access the attributes of a variable.
How to use variables
We use variables inside the templates by passing any Python object to the render()
function.
return render(request,"index.html", context = any_object)
In the example above, index.html
is the name of the template and context
is the name of the object we are passing.
Get hands-on with 1400+ tech skills courses.