KVISH
KVISH

Reputation: 13178

django radio buttons in forms

I have a form in Django with radio buttons that have various properties for users. When an admin user (in my application...not the django admin) wants to edit properties for different users, the same form objects should appear for all users in a list. So say there is a list of possible permissions for users, I have a table with the list of users, and the user permissions options (radio buttons) should appear next to them. In the form I have a radio button choice field with all available permissions. But in the view, how do I get it to appear for each user? Also, how do I set initial data when there're are many of the same form object? Is there an example of this somewhere? Thanks!

[EDIT]

Below is basically what I am trying to show:

    <table class="admin_table">
        <tr>
            <th>Firstname</th>
            <th>Lastname</th>
            <th>User Permission</th>
        </tr>
        {% if users|length > 0 %}
            {% for user in users %}
                <tr>
                    <td>{{ user.first_name }}</td>
                    <td>{{ user.last_name }}</td>
                    <td>[RADIO BUTTONS]</td>
                </tr>
            {% endfor %}
        {% endif %}
    </table>

The place where I wrote [RADIO BUTTONS], is where I woud like to show multiple radio buttons which should appear for each user, not for the whole form.

Also, I'm looking at this site: http://jacobian.org/writing/dynamic-form-generation/. Not sure if that's the solution, but the problem the author is trying to solve is very interesting anyway.

Upvotes: 0

Views: 1764

Answers (1)

Rohan
Rohan

Reputation: 53316

Maybe you can try something like this:

The key is to group the radio button inputs for each user using {{user.id}}. You can name them as you want.

{% for user in users %}
    <tr>
      <td>{{ user.first_name }}</td>
      <td>{{ user.last_name }}</td>
      <td><input type="radio" name="user_{{user.id}}" value="Radio1" >Radio1</input></td>
    </tr>
{% endfor %}

Upvotes: 2

Related Questions