Using context variable to name a "section" instead of passing a value

Justin

In the following view, the context variable is used to mark the 'section' as 'dashboard'(I think).

@login_required
def dashboard(request):
    return render(request, 'account/dashboard.html', {'section': 'dashboard'})

This then allows me to say {% if section == 'whatever' %}, for example:

{% if request.user.is_authenticated %}
      <ul class = 'menu'>
        <li {% if section == 'dashboard' %} class='selected' {% endif %}>
          <a href="{% url 'dashboard' %}">My Dashboard</a>
        </li>
        ... continues
    </ul>
   {% endif %}

Now if the dashboard view is called, .selected will be applied to the 'Dashboard' link (change color to show it's active). I get what it is doing, just don't understand how this works. I have only seen values passed in the context i.e. {'number': 8}. How exactly does django keep track of this naming system? It does not seem to be adding a class or anything of the sorts.

ivissani

Every key in the context dictionary that is passed to the template is transformed into a variable with that very same name that is available to the template at the moment of rendering.

For example if you render a template with a context dict such as:

{
    'key1': 'somevalue1',
    'key2': 'somevalue2',
    'key3': 'somevalue3',
}

in the template, at rendering time, you will have three variables called key1, key2 and key3 with the corresponding values.

So what is happening in your code is that when you call the view dashboard() this view renders the template your view calls the render() method which in turn takes the template code defined in the html file you pass to it and converts this code into pure html by executing the django template code in a context where a variable section has been created and assigned the value 'dashboard' (because that's what you passed to render() as the context param)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why is the object property using variable name as key instead of variable value?

Can't use $refs using variable name instead of direct value

passing an element name using a variable

ssh passing and receiving a variable value instead of a file

django-component - passing string instead of context variable

Assigning a function to the value of a variable instead of the variable name

Passing a variable using async/await instead of a subscribe

jQuery using variable name instead id name

display variable name instead of value in foreach loop

Indirect expansion returns variable name instead of value

Python get variable name instead of value

Prolog returns variable name instead of value

Variable passing context

Passing in name in function and using it as a value in a Pandas column

PHP url variable passing prints variable name not the value

JavaScript function not working when passing variable instead of hard coded value

Using value of a variable as a property name

jQuery Validation using the class instead of the name value

Passing value by using Context API and useContext (React js)

Adding variable name instead of value of variable to select list

Lua puts of variable name instead of variable value in table

How do i print Variable name instead of the value assigned to the variable?

Using Jquery $("a[href*='.sometext']") passing variable instead of static string

PHP using variable value in a variable name

Using variable value as variable name in bash

Variable value in variable name using bash

Using a value instead of variable in the html for a directive attribute

Passing a variable value, rather than its name, as a class

Array Name passing variable