djangoformsstylingdjango-bootstrap4

My form does not save using bootstrap for styling in django


hello thank you for your visiting I'm a new learner of Django I would like to know how to style form using django-bootstrap-v5 i try this and does not work

i have this form.py

pathesForm = inlineformset_factory(
    Book,
     quotation,
      fields=('name','time',),
      can_delete=False,extra=4,max_num=4,
      widgets={'name': forms.TextInput(attrs={
            'placeholder': 'name of book',
        })
    }
    
      )

i use django-bootstrap-v5 and in file html

this form is not working with me

<form role="form" class="form-horizontal" method="post">
        {% csrf_token %}
      {{ formset.management_form }}
        {% for form in formset %}
          <div class="row">
        <div class="col-md-6">
          {%  bootstrap_field form.name %}
        </div>
        <div class="col-md-6">
          {%  bootstrap_field form.time %}
        </div>
      </div>
      {%  if form.maybemissing %}
        {%  bootstrap_field form.maybemissing %}
      {%  endif %}
{% endfor %}

        <button type="submit">Save</button>
    </form>

but this is working with me (i can save the form)

{% bootstrap_formset_errors formset %}
    <form role="form" class="form-horizontal" method="post">
        {% csrf_token %}
    
          {%  bootstrap_formset formset %}
      
        <button type="submit">Save</button>
    </form>

this is my view.py

def hello(request,id):
    book=Book.objects.get(id=id)
        if request.method == 'POST':
            form= pathesForm(request.POST,request.FILES,instance=book)
            if form.is_valid():

                form.save()
        
            
        form = pathesForm(instance=book )
        

        return render(request,'hello/pathesForm.html',{'formset':form})

i use print('hello) to try know where is the problem and the result seems like the form is not valid how i can to customize the style of my form like the first one


Solution

  • allways if you have a problem with from use print(form.errors) before the form is valid and after it i got my problem and i got this error [{'id': ['This field is required.']} soulition:

        <form role="form" method="post">
            {% csrf_token %}
          {{ formset.management_form }}
            {% for form in formset %}
              {% for hidden in form.hidden_fields %}
            {{ hidden }}
        {% endfor %}
              <div class="row">
            <div class="col-md-6">
              {%  bootstrap_field form.name %}
            </div>
          </div>
          {%  if form.maybemissing %}
            {%  bootstrap_field form.maybemissing %}
          {%  endif %}
    {% endfor %}
    

    i think it success using formset only because of ( id add automatically ) and if you want to forloop you should be add it manually