MinionAttack
MinionAttack

Reputation: 540

List item template Django

I'm dealing creating a template on Django to show a list of items with 2 buttons that make actions.

My form class it's:

class AppsForm(forms.Form):
    def __init__(self, *args, **kwargs):
        policiesList = kwargs.pop('policiesList', None)
        applicationList = kwargs.pop('applicationList', None)
        EC2nodesList = kwargs.pop('amazonNodesList', None)
        super(AppsForm, self).__init__(*args, **kwargs)
        self.fields['appsPolicyId'] = forms.ChoiceField(label='Application Policy', choices=policiesList)
        self.fields['appsId'] = forms.ChoiceField(label='Application', choices=applicationList)
        self.fields['ec2Nodes'] = forms.ChoiceField(label='Amazon EC2 Nodes', choices=EC2nodesList)

Now, I do the form with:

<form method="post" action="" class="form-inline" role="form">
      <div class="form-group">
          {% for field in form %}
          { field.label }}: {{ field}}
          {% endfor %}
      </div>
     {% csrf_token %}
     <input type="submit" class="btn btn-default btn-success" name="deployButton" value="Deploy"/>
     <input type="submit" class="btn btn-default btn-danger" name="undeployButton" value="Undeploy"/>

And the result it's:

Application Policy - Choicefield ; Application - Choicefield ; Amazon EC2 Nodes - Choicefield [Button Deploy] [Button Undeploy]

And what I'm looking for it's a way to render the form and show the list like this:

Application Policy - Choicefield ; Application - Choicefield [Button Deploy] [Button Undeploy]
Amazon EC2 Nodes - Choicefield [Button Deploy] [Button Undeploy]
<more items if I add them in forms.py...>

How I can get the proper way to render like that?

Thanks and regards.

Upvotes: 0

Views: 53

Answers (1)

Michael Platt
Michael Platt

Reputation: 1342

You just need to change the code a bit is all:

{% for field in form %}
    { field.label }}: {{ field}}
    <input type="submit" class="btn btn-default btn-success" name="deployButton" value="Deploy"/>
    <input type="submit" class="btn btn-default btn-danger" name="undeployButton" value="Undeploy"/>
    <br />
{% endfor %}

So this will create a new line for each of the field.label and field variables with their own button. One thing to caution against though, if you try and assign ID's to the buttons they will have to be different or you'll get errors. Also, submission may be a bit weird with code such as this but it depends on the rest of your application. Either way, this will give you the desired format.

Upvotes: 1

Related Questions