1

How to create on one page multiple form with instance of each model object. I have already created object of salon. And i need to render form with instance of all objects on one page.

forms.py

class SalonForm(forms.ModelForm):

    class Meta:
        model = Salon
        fields = ('some_fields')

views.py

def salon_list(request):
    salons_list = Salon.objects.all()
    form = SalonForm()
    ctx = {
        'salons': salons,
        'form': form,
    }
    return render(request, 'template/list.html', ctx)

list.html

{% for salon in salons %}
    {{ salon }}
    {{ form }}
{% endfor %}
3
  • i mean i need to render form with instance of each object. Commented Mar 16, 2019 at 18:09
  • Can you give a example Commented Mar 16, 2019 at 18:10
  • 1
    Model formsets Commented Mar 16, 2019 at 18:33

2 Answers 2

2

You can use FormSet.

from django.forms import modelformset_factory

formset link

Sign up to request clarification or add additional context in comments.

2 Comments

okay its worked, but also i need provide data from model(without form inputs), Like salon - name, can i somehow get this data? in formset
in view: add on top salon = Salon.objects.filter(... and extend {'formset': formset, 'salon': salon}
0

To put the model formset solution together:

forms.py

class SalonForm(forms.ModelForm):

    class Meta:
        model = Salon
        fields = ('some_fields')

views.py

from django.forms import modelformset_factory

def salon_list(request):
    SalonFormSet = modelformset_factory(Salon, form=SalonForm)

    if request.method == 'POST':
    formset = SalonFormSet(request.POST)
        if formset.is_valid():
        formset.save()
    else:
        formset = SalonFormSet()

    return render(request, 'template/list.html', {'formset': formset})

list.html

<form method="post">
    {{ formset }}
</form>

The links for more Details (QuerySets, render each field individual ...) are already above.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.