[Python] Django - Dictionary value set to returned as `None` in template but returns value in...

Discussão em 'Python' iniciado por Stack, Março 9, 2025.

  1. Stack

    Stack Membro Participativo

    In my console I have this print: r_distance in context: {13: 7905.59}

    In my template, I get the following returned {13: None} , using:

    r_distance: {{ r_distance}}


    I dont understand what would turn the value to None in the my logic.

    views.py:

    for r in rps:
    if r.model_field:
    ref_v = rewardprogram.model_field

    if ref_v.latitude and ref_v.longitude and user_lat and user_lon:
    v_distance = distance.distance((user_lat, user_lon), (ref_v.latitude, ref_v.longitude)).km
    v_distance = round(v_distance, 2)
    r_distance[model.id] = v_distance # prints: {13: 7905.59}

    else:
    r_distance[model.id] = None
    context = {
    ...
    "r_distance": r_distance}
    }


    In my template:

    {% for r in rps%}
    <p>r_distance: {{ r_distance}}</p> # prints: {13: None}
    {% endfor %}


    Question: Considering r_distance is recognised in the template, it cannot be that the dictionaryy is not being passed to the template. The model object id is correct (13), why would the value not getting passed?

    Continue reading...

Compartilhe esta Página