2

I have django choice field

The HTML rendered by django looks like this:

<select id="id_action" name="action">
 <option value="" selected="selected">---</option>
 <option value="query-action-take">Take</option>
 <option value="query-action-forward">Forward</option>
</select>

Before executing the action which was choosen by the user, a confirmation should happen. But not all actions need a confirmation. In this example "delete" needs, but "take" does not need a confirmation.

I would like to add html data attributes the options

The result should look like this:

<select id="id_action" name="action">
 <option value="" selected="selected">---</option>
 <option value="query-action-take"   data-need-confirm="False">Take</option>
 <option value="query-action-delete" data-need-confirm="True">Delete</option>
</select>

Up to now I found no way to inject the data "need confirmation" into individual choices.

... or am I on the wrong track here?

This question is only about "how to get the data into the html". The evaluation of the data (confirmation popup) is not part of this question.

guettli
  • 25,042
  • 81
  • 346
  • 663
  • have you tried using JS or jquery to add `data` to your html? – Charlesliam Mar 29 '17 at 10:59
  • you start a action based on what option node is selected, correct? isn't it easier to handle the confirmation inside javascript then? i think option tags can not handle data attributes because they are very special. – mtizziani Mar 29 '17 at 10:59
  • @mtizziani yes, I will do the confirmation inside javascript. This question is about how to get the data into the html. The evaluation is no part of this question. – guettli Mar 29 '17 at 11:03
  • @guettli perhaps you'r looking for a sollution like this: https://jsfiddle.net/vyhb2c2z/ – mtizziani Mar 29 '17 at 11:08
  • @mtizziani this question is about putting the data into the html. Your solution is 100% correct. But your solution is the answer to a different question :-) – guettli Mar 29 '17 at 11:37
  • 1
    There is a general solution which works for Django 2.+ and allows to add a title and other things in options, see https://stackoverflow.com/a/56097149/1788851 – Edouard Thiel May 12 '19 at 12:07

2 Answers2

4

You need to create a custom Widget for this. If you check out the Django docs for the Select widget (django.forms.Select), you will find a render_option() method. So best to subclass the default Select class and override this method.

dentemm
  • 6,231
  • 3
  • 31
  • 43
  • Note that `Select.render_option()` was removed in Django 1.11 due to the introduction of [template-based *widget* rendering](https://docs.djangoproject.com/en/4.1/releases/1.11/#changes-due-to-the-introduction-of-template-based-widget-rendering). – djvg Feb 22 '23 at 14:00
0

The template renderer definitely contains the functionality to add the data, the question is where you get it from.

<select id="id_action" name="action">
 <option value="" selected="selected">---</option>
 {% for action in actions %}
  <option value="{{action.value}}"   data-need-confirm="{{action.isConfirm}}">{{action.name}}</option>
 {% endfor %}
</select>

Would this be something you want to add to a model? Probably not. But you can add it as a hard coded dictionary too.

Eaton Emmerich
  • 452
  • 4
  • 17
  • I don't like templates too much. I somehow prefer to write a custom widget. Please don't ask me why. This is a feeling, no rational decision. – guettli Mar 29 '17 at 12:18
  • 1
    Without Templates or Javascript you will have to override the behavior of the widget class as stated by dentemm. – Eaton Emmerich Mar 29 '17 at 15:21