0

I have a django app that returns a list of table rows into a html table and one of the fields is supposed to be an selector with a set of predefined values and selected value has to be the one provided as a variable from django app. Here's the code:

<table>
    <thead>stuff here</thead>
        <tbody style="font-size: 10px">
        {% for ticket in scope %}
        <tr>
            <td id="tId" name="tId">{{ ticket.0 }}<input type="checkbox" id="accept" name="accept" value="{{ ticket.0 }}"/> <br />Select</td>
            <td hidden><input name="list_{{ ticket.0 }}" value="list_{{ ticket.0 }}"/></td>
            <td>
                <select id="lvl4" name="lvl4">
                    <option id="ticket1" value="{{ ticket.1 }}">{{ ticket.1 }}</option>
                </select>
            </td>
            <td>{{ ticket.2 }}</td>
            <td>{{ ticket.3 }}</td>
            <td>{{ ticket.4 }}</td>
            <td>{{ ticket.5 }}</td>
        </tr>
        {% endfor %}
        </tbody>
</table>

Set of available values:

  • value1
  • value2
  • value3
  • value4 "wrong ticket type"

Set of values retrieved from the SQL table:

  • value1
  • value2
  • value3
  • value4 (select the "wrong ticket type" option)
  • value5 (select the "wrong ticket type" option)

What I want to achieve with this select field is to select {{ ticket.1 }} matching option or the "wrong ticket type" if there is no matching option.

Please let me know if more information is needed. Thank you for your support.

EDIT: Code of the select tag I would like to achieve:

<select id="lvl4" name="lvl4">
<option value="value1">value 1</option>
<option value="value2">value 2</option>
<option value="value3">value 3</option>
<option value="value4">wrong ticket type</option>
</select>

With option value selected that equals the {{ ticket.1 }} value or "value4" if no match.

4
  • {{ ticket.1|default:"wrong ticket type" }} ? Commented Aug 23, 2016 at 8:28
  • @AnnaVracheva, could you explain your code a little bit? I'm not very experienced in Django and don't know how it solves my problem. Commented Aug 23, 2016 at 8:32
  • I not sure that totally understand your problem. But if you need to display {{ ticket.1 }} and if it's not exists or null, display some default text, you can use template tag default. Commented Aug 23, 2016 at 8:47
  • Your question isn't very clear. You want the output in a table or a select widget in a form? For the second, use a django form (probably with some logic in the init method for deciding on matching options). And what do you mean by a "matching option" what has to match what? Commented Aug 23, 2016 at 20:41

1 Answer 1

1

Some comments suggested my question is not clear which I agree it could be the case. Please forgive me, English is not my native language, but I'm trying :)

In the meanwhile a colleague of mine provided me with a solution I was looking for.

And here it is:

<select name="lvl4_{{ ticket.0 }}">
<option {% if ticket.1 == "Run Support" or ticket.1 == "User Support" %}selected {% endif %} disabled>incorrect ticket type</option>
<option {% if ticket.1 == "Change Request" %}selected {% endif %} value="Change Request">Change Request</option>
<option {% if ticket.1 == "Internal Ticket" %}selected {% endif %} value="Internal Ticket">Internal Ticket</option>
<option {% if ticket.1 == "Enhancement Request" %}selected {% endif %} value="Enhancement Request">Enhancement Request</option>
<option {% if ticket.1 == "Technical Request" %}selected {% endif %} value="Technical Request">Technical Request</option>
</select>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.