How to add a table in Django Cryspy forms?

Viewed 444

I would like to add a table to a Django crispy form. I am trying to add the following html code to my create_bill.html or forms.py but I keep encountering some syntax errors. what is the correct way to add the following html code and remove the title for each linesAdd lines table:

<div class="card-block table-border-style">
     <div class="table-responsive">
         <table class="table table-hover">
             <thead>
                 <tr>
                    <th class="center">Item</th>                                                    
                    <th>Description</th>
                    <th class="center">Qty</th>
                    <th class="right">Unit Price</th>
                    <th>Account</th>
                    <th>Tax Rate</th>
                    <th class="right">Amount GBP</th>
                </tr>
             </thead>                                            
          </table>
       </div>
  </div>

forms.py

class BillForm(forms.ModelForm):

    bill_title = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Title'}))
    vendor =  forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Vendor'}))
    bill_date =  forms.DateField(widget=forms.DateInput(attrs={'placeholder': 'Bill Date'}))
    due_date = forms.DateField(widget=forms.DateInput(attrs={'placeholder': 'Due Date'}))
    reference = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Reference'}))

    class Meta:
        model = Bill
        fields = ['vendor','bill_date','due_date','reference']  


    def __init__(self, *args, **kwargs):
        super(BillForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_tag = True
        self.helper.layout = Layout(
            Row(
                Column('vendor', css_class='form-group col-md-2 mb-0'),
                Column('bill_title', css_class='form-group col-md-2 mb-0'),
                Column('bill_date', css_class='form-group col-md-2 mb-0'),
                Column('due_date', css_class='form-group col-md-2 mb-0'),
                Column('reference', css_class='form-group col-md-2 mb-0'),
                css_class='form-row'
            ),            
            Fieldset('Add lines',Formset('lines')),
            Row(                               
                css_class='form-row'
            ),
            Div(               
                HTML("<br>"),
                ButtonHolder(Submit('submit', 'save')),
                )
            )

create_bill.html

{% extends 'base.html' %}

{% load crispy_forms_tags %}

{% block content %}
  {% crispy form %}
{% endblock %}
3 Answers

Do you write "CRISPY_TEMPLATE_PACK = 'bootstrap4'" in the settings.py?

I am using like this. Can you try like this in html page?

{% extends 'base.html' %}
    {% load static %}
    {% block body %}
    {% load crispy_forms_tags %}
    
    
    <form method="post">
        {% csrf_token %}
        {{form|crispy}}
    <br>
        <button type="submit" class="btn btn-danger">Kayıt Ol</button>
    </form>

Old question, i know. But have you tried using a classic bootstrap html table with and tags ?

Something like :

<table class="table table">
    <thread class="border-bottom font-weight-bold">
        <tr>
            <td class="col-md-4" style="text-align: center">Column 1</td>
            <td class="col-md-4" style="text-align: center">Column 2</td>
            <td class="col-md-4" style="text-align: center">Column 3</td>    
        </tr>
    </thread>
    <tbody>
        {% for form in formset %}
            <tr>
                <td class="col-md-4" style="text-align: center">{{form.Column1}}</td>
                <td class="col-md-4" style="text-align: center">{{form.Column2}}</td>
                <td class="col-md-4" style="text-align: center">{{form.Column3}}</td>
            </tr>
        {% endfor %}
    </tbody>
</table>

You need to include this within your form.

Related