Paul Zet
Paul Zet

Reputation: 71

How to create and submit multiple instances of a form on a single page?

I want 3 instance of a URL input form so I can submit up to 3 different URLs.

forms.py

class AdditemForm(forms.Form):
    url = forms.URLField(
        label='Add Item',
        widget=forms.URLInput(
            attrs={
                "class": "form-control",
                }))

view.py

def ItemDetail(request, pk):
    listitem = comparelist.objects.get(id=pk)
    if request.method == 'POST':
        form = AdditemForm(request.POST)
        if form.is_valid():
            url = form.cleaned_data.get("url")                
            items.objects.create(
                    link=url,
                    name=product_name,
                    price=price,
                    store=store,
                )
            return HttpResponseRedirect(request.path_info)
    else:
        form = AdditemForm()
    template = 'itemdetail.html'
    context = {
        "comparelist": listitem,
        "form": form,
    }
    return render(request, template, context)

I'm using a form snippet I found in a tutorial:

{% load widget_tweaks %}
<form method="post" class="form">
            {% csrf_token %}
            {% for hidden_field in form.hidden_fields %}
                  {{ hidden_field }}
                {% endfor %}

                {% if form.non_field_errors %}
                  <div class="alert alert-danger" role="alert">
                    {% for error in form.non_field_errors %}
                      {{ error }}
                    {% endfor %}
                  </div>
                {% endif %}

                {% for field in form.visible_fields %}
                  <div class="form-group">
                    {{ field.label_tag }}

                    {% if form.is_bound %}
                      {% if field.errors %}
                        {% render_field field class="form-control is-invalid" %}
                        {% for error in field.errors %}
                          <div class="invalid-feedback">
                            {{ error }}
                          </div>
                        {% endfor %}
                      {% else %}
                        {% render_field field class="form-control is-valid" %}
                      {% endif %}
                    {% else %}
                      {% render_field field class="form-control" %}
                    {% endif %}

                    {% if field.help_text %}
                      <small class="form-text text-muted">{{ field.help_text }}</small>
                    {% endif %}
                  </div>
                {% endfor %}
            <button type="submit" class="btn btn-primary">Submit</button>
        </form>

So how do I get 3 of those forms on my page and be able to submit 3 different URLs? I can only think of having to create 3 different form classes and paste the form snippet 3 times into the template. But that seems like a lot of unnecessary repetition.

Upvotes: 0

Views: 319

Answers (1)

bruno desthuilliers
bruno desthuilliers

Reputation: 77902

Why "create 3 different form classes" ??? You can just create three instances of the same form.

paste the form snippet 3 times into the template

Ever heard about lists and loops ? You can put the three (or more) forms in a list and loop over it.

BUT that's actually not the best solution here - Django has Formsets for this use case.

Upvotes: 2

Related Questions