In django how to access 2 field values in custom django modeladmin...includes/fieldset.html?

98 Views Asked by At

Edited my code: In the custom fieldset of a model admin:

{%load app_extras %}

{% if field.field.name == 'mobile' %}
    <a target="hiddenIframe" href="http://url_to_call.php?exten={{request.user.employee_profile.extension}}&phone={{ field.field.value }}">Click-to-call</a>
{% my_mobile mobile=field.field.value as mob %}
    {% endif %}

    {% if field.field.name == 'sms_message' %}{{ mob }}
        <a target="hiddenIframe" href="http://url_for_send_sms.php?sms_message={{ field.field.value }}&phone={{ mob }}">Click-to-send-sms</a>
    {% endif %}

Here I am trying to access mobile number as well as sms_message fields of the model admin form simultaneously.

I have figured that I need to use custom tags, so I created the templatetags module, with app_extras.py containiging the function to assign the value of mobile and return it as follows:

@register.assignment_tag
def my_mobile(*args, **kwargs):
    m_mobile = int(kwargs['mobile'])
    return {'m_mobile': m_mobile }

In the template fiedset.html above note changes: This returns a Long value as: {'m_mobile': 1234534519L} When seen on the browser for url for hyperlink shows:

http://url_for_send_sms.php/?sms_message=fgdfg&phone={%27m_mobile%27:%1234534519L}

How do I access the mobile number? Is my custom tag correct?

2

There are 2 best solutions below

0
user956424 On BEST ANSWER

I formatted the output in my tag as:

@register.assignment_tag
def my_mobile(*args, **kwargs):
    m_mobile = ("%d" %int(kwargs['mobile']))
    return {'m_mobile': m_mobile }

In the template fieldset.html changed the code as:

{% if field.field.name == 'sms_message' %}
        <a target="hiddenIframe" href="http://url_for_send_sms.php?sms_message={{ field.field.value }}&phone=={{ mob.m_mobile }}">Click-to-send-sms</a>
    {% endif %}

Important: Both the mobile number and the sms_message are in the same line of the fieldset in the django modeladmin (in my case). So above code belongs to the loop {% for line in fieldset %} loop

0
simopopov On

Try

{% for ln in fieldset %}
    {% for fld in ln %}
        {% if f.field.name == 'mobile' %}
            {{ f.field.value }}
        {% endif %}
    {% endfor %}
{% endfor %}

Maybe this is not the best solution ... but it is solution :)