Home > OS >  Setting value to default in a django for loop
Setting value to default in a django for loop

Time:07-22

I've been stuck on a rather simple issue, where i'm trying to either set a value on a variable in a for loop, or setting the default data to N/a.

I set up a datatable that gathers lots of data, but i need to return 2 of the variables to N/a if not found in the for loop.

In the code example below, you can see a generic example of what i've tried. In the 2 for loops i'm trying to set a global var, that i can than access outside of the for loop.

I've done some reading and i just can't figure out the smartest way of doing this.

Hence me asking the question here.

Please do ask if you need additional information.

                {% for vlan, macs in info.vlans.items %}
                {% for mac in macs %}
                {% endfor %}
            {% endfor%}
            
            {% if vlan %}
            <td>{{vlan}}</td>
            {% else %}
            <td>N/a</td>
            {% endif %}

            {% if mac %}
            <td>{{mac}}</td>
            {% else %}
            <td>N/a</td>
            {% endif %}

I did also try to set the values within the for loop, but it won't work for the ones that do not have a mac or vlan assigned, as it does not set the default of N/a.

                {% for vlan, macs in info.vlans.items %}
            {% if vlan %}
            <td>{{vlan}}</td>
            {% else %}
            <td>N/a</td>
            {% endif %}
                {% for mac in macs %}
            {% if mac %}
            <td>{{mac}}</td>
            {% else %}
            <td>N/a</td>
            {% endif %}
                {% endfor %}
            {% endfor%}

CodePudding user response:

Compare it with None, that should help:

{% if mac is None %}
    <td>N/a</td>
{% else %}
    <td>{{mac}}</td>
{% endif %}

also you can try built-in default_is_none filter:

{{ mac|default_if_none:"N/a" }}

CodePudding user response:

I have solved it by just seperating it.

Instead of what i tried to do before, i kind of simplyfied it. It's not the cleanest type of code, but for now this works.

                {% for switch, port in result.items %}
                {% for interface, info in port.items %}
                {% for vlan, macs in info.vlans.items %}
                {% for mac in macs %}
                <tr>
                    <td>{{info.outlet}}</td>
                    <td>{{info.ruimte}}</td>
                    <td>{{switch}}</td>
                    <td>{{interface}}</td>
                    <td>{{ vlan|default_if_none:"N/a" }}</td>
                    <td>{{ mac|default_if_none:"N/a" }}</td>
                    {% endfor %}
                {% endfor%}
                {% endfor%}
                {% endfor%}

                {% for switch, port in result.items %}
                {% for interface, info in port.items %}
                <tr>
                <td>{{info.outlet}}</td>
                <td>{{info.ruimte}}</td>
                <td>{{switch}}</td>
                <td>{{interface}}</td>
                <td>N/a</td>
                <td>N/a</td>

I'd like to thank you for helping me out nevertheless, i never knew u couldn't set/get variables in django.

  • Related