简体   繁体   中英

Django Form looping through form fields but display 1 of each field in html row

I have a form which dynamically creates a number of fields based on the number of a variable in my HTML I then loop through the form to display the fields but I need to be like so:

row 1 = "Field 1, Field 2"

instead it is like:

row 1 = "Field 1, Field 1" row 2 = "Field 2, Field 2"

Form Code:

class AttendingForm(forms.ModelForm):

class Meta:
    model = Attending
    fields = ('name', 'type')

def __init__(self, *args, **kwargs):
    self.ticket_count = kwargs.pop('count')
    super(AttendingForm, self).__init__(*args, **kwargs)
    for i in range(1, self.count):
        self.fields['%d name' % i] = forms.CharField()
        self.fields['%d type' % i] = forms.ChoiceField()

HTML Code Snippet:

    <form method="post">
    <section>
        <h1>Manage Attendees</h1>
        <div class="content-section">
            {% for field in form %}
                <div class="form-field">
                    <label for="visitor_name">Name
                        {{ field }}
                    </label>
                </div>
                <div class="form-field">
                    <label for="visitor_name">Type</label>
                        {{ field }}
                </div>
            {% endfor %}
            <div class="form-field">
                <input type="submit" value="Submit" class="button button-black">
            </div>

        </div>

    </section>
    </form

I don't see an easy way to do this without adding a property to your form. In your __init__() method, at the end, add a new list of dicts:

self.ordered_fields = [{
    'name': self.fields['%d name' % i],
    'type': self.fields['%d type' % i]} for i in range(1, self.count)]

Then in your template:

{% for item in form.ordered_fields %}
    Name: {{ item.name }}
    Type: {{ item.type }}
{% endfor %} 

Note: Don't use spaces in your field names, that might cause issues in your submitted data. Use '%d-type' instead of '%d type' .

I've adjusted the form to a forms.Fom and set the label of each name field, then set some if statements in the html to order correctly.

Form Code:

class AttendingForm(forms.Form):
    # class Meta:
    #     model = Attending
    #     fields = ('attendee_name', 'booking_type')

    def __init__(self, *args, **kwargs):
        self.ticket_count = kwargs.pop('count')
        super(AttendingForm, self).__init__(*args, **kwargs)
        for i in range(1, self.count):
            self.fields['%d name' % i]= forms.CharField(label="attendee_name")
            self.fields['%d type' % i] = forms.ChoiceField(choices=(
                ('-------', '-------'),
                ('adult', 'Adult'),
                ('child', 'Child'),
            ))

HTML Code:

<form method="post">
<section>
            <h1>Manage Attendees</h1>
            <div class="content-section">
                {% for field in form %}
                    {% if field.label == 'attendee_name' %}
                        <div class="form-field">
                            <label for="visitor_name">Name</label>
                            {{ field }}
                        </div>
                    {% else %}
                        <div class="form-field">
                            <label for="visitor_name">Type</label>
                            {{ field }}
                        </div>
                    {% endif %}
                {% endfor %}
                <div class="form-field">
                    <input type="submit" value="Submit" class="button button-black">
                </div>

            </div>

        </section>
        </form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM